i want to create my own 404 page.
In settings.py I have added:
DEBUG = TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['*',]
In urls.py:
handler404 = 'blog.views.handler404'
In views.py:
def handler404(request):
return render(request, 'blog/404.html')
Also I have created that 404.html file.
When i start server i write:
python manage.py runserver --insecure
--insecure is to provide static files (otherwise it is nonsense). But if i go non existing page i get:
<h1>Not Found</h1><p>The requested URL /post/9/ was not found on this server.</p>
How do I solve this?
I am using Django 1.8 dunno if this changes anything
You shouldn't need anything in urls.py. Go to your root views.py and add your handler404 method there, and leave urls.py alone.
Ref: Django, creating a custom 500/404 error page
Also, I don't see your TEMPLATE_DIRS variable, i.e.
TEMPLATE_DIRS = (
'/path/to/template/files/',
'/path/to/more/template/files/'
)
Need to make sure that your templates in ../blog/.. are getting found properly. Personally I'd add that specifically as a subdirectory.
Related
Normaly django looks for a url in url.py file...
if it finds it load the relevent HTML page and if it doesn't find it shows:
Page not found (404) with the msg:
You're seeing this error because you have DEBUG = True in your Django
settings file. Change that to False, and Django will display a
standard 404 page.
I want to change standard 404 page that django shows. I have a HTML page that I created PageNotFound.html I want django to show everytime there is a Page not found (404) when DEBUG = False. How do I do that?
In order to show customized HTML when Django returns a 404, you can create an HTML template named 404.html and place it in the top level of your template tree. This template will then be served when DEBUG is set to False.
https://docs.djangoproject.com/en/dev/topics/http/views/#the-404-page-not-found-view
Also, there is an useful section "Customizing error views".
And in the case you want to test your template withour turning DEBUG=False, you can run manage.py runserver --insecure.
It will force Django to serving static files with the staticfiles app even if the DEBUG setting is False. But note that it it only for local development and could be insecure. You can read more about this option here.
Here is a quote from the Django doc:
This template should be called 404.html and located in the top level of your template tree.
Check here for the full text; for non 1.8 doc it should be somewhere around here: Writing views > Returning errors > The Http404 exception.
You should create following view:
def custom404(request):
return render(request, '404.html', status=404)
And connect it with following construction in urls.py:
handler404 = 'path.to.views.custom404'
This is my folder structure
Music
-Music
-Feature
-static
-feature
core.css
-css
other css files
-js
-img
-templates
404.html
500.html
index.html
-feature
about.html
detail.html
template.html
manage.py
views.py
from django.shortcuts import render
def error404(request):
return render(request,'404.html')
urls.py
urlpatterns = [
url(r'^featured/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^about/$', views.about, name='about'),
url(r'^FAQ/$', views.faq, name='faq'),
]
handler404 = 'mysite.views.error404'
The custom 404.html file gets rendered but with no css.And normally the css works fine on other pages but when I set debug=falseto check the custom 404 error page in settings.py the css for the entire project disappears. Something to do with folder structure or some other problem?
edit: core.css is the main css file and the part with other css files contains css for plugins
It's about serving static files. When you use DEBUG = True then django takes care about them otherwise your server should do it. Django in debug mode uses this view. The warning from there:
This view will only work if DEBUG is True.
That’s because this view is grossly inefficient and probably insecure.
This is only intended for local development, and should never be used
in production.
You can run your server with --insecure option just to test 404 error or you can explicilty create url for that page to check its styling:
Use the --insecure option to force serving of static files with the
staticfiles app even if the DEBUG setting is False. By using this you
acknowledge the fact that it’s grossly inefficient and probably
insecure. This is only intended for local development, should never be
used in production and is only available if the staticfiles app is in
your project’s INSTALLED_APPS setting. runserver --insecure doesn’t
work with CachedStaticFilesStorage.
Your handler404 and view are okay. But you don't need them. Just a custom 404 template is enough. See: https://docs.djangoproject.com/en/1.8/topics/http/views/#the-http404-exception
In order to use the Http404 exception to its fullest, you should
create a template that is displayed when a 404 error is raised. This
template should be called 404.html and located in the top level of
your template tree.
The template is in the right location. I think the problem is serving your static files. Open developer tools in your browser to see what resources fail to load (console, network or sources tab). Inspect the paths. Is there an external style sheet link in the head section of the 404 source? (elements tab or view source code).
https://docs.djangoproject.com/en/1.8/howto/static-files/
I want to use django's password change/reset views so for example for the view
django.contrib.auth.views.password_change
I need create a template named
registration/password_change_form.html
the problem is that even though I have this template implemented in my project, django still shows the password-change page of the admin website, the only way I can make django use my template is by renaming it to something different - like registration/password_change_form_1.html and then pass the name
url(r'^password/change/$',
auth_views.password_change,
{'template_name': 'registration/password_change_form_1.html',
'password_change_form': MyPasswordChangeForm},
name='password_change'),
Am I missing something here? why won't django use my template when I use the default name?
I think because your app is under django.contribute.admin in the INSTALLED_APP.
Django automatically generates the admin template with the default name, so, if you use the admin, you must specify a different template name.
It simply fails to find your template, since it is overiden by the generated one.
Add in settings
INSTALLED_APPS = (
...
'registration',
)
After
TEMPLATE_DIRS = (
...
"/home/user/templates",
)
Add in directory templates "registration"
base.html that will contain the template and other templates
run in django==1.5
I am using Django 1.5.4 and I have a project with the following structure:
django_site
-django_site
-books # the app
-media
-images
-books
-authors
-static
-images
-templates
This is the necessary code:
# settings.py
MEDIA_ROOT = '/root/django_site/books/media/'
MEDIA_URL = '/media/'
# models.py
class Book(models.Model):
image = models.ImageField(upload_to="images/books/")
# urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^books/$', 'books.views.get_all_books'),
)
My problem is that, after adding the image via the admin site, if I click on the image link below the upload button a 404 page shows up, complaining the path does not exist.
The request URL is http://127.0.0.1:8000/media/books/media/images/book/out_of_control_1.JPG but in fact I want the result to be /root/django_site/books/media/images/out_of_control_1.JPG.
How do I fix that? Looking forward to your responses.
Since you seem to use the developpement server, I think your issue is related to this one : Django MEDIA_URL and MEDIA_ROOT
As explained in the answer, you have to set up a specific URL pattern for media handling when debug is set to True.
You can also have a look at the docs regarding this question.
I'm using the staticfiles app during development, which doesn't work unless DEBUG is turned on.
From the docs:
Warning This will only work if DEBUG is True.
That's because this view is grossly inefficient and probably insecure.
This is only intended for local development, and should never be used
in production.
Additionally, when using staticfiles_urlpatterns your STATIC_URL
setting can't be empty or a full URL, such as
http://static.example.com/.
I'm trying to view my Http404 templates, though, and of course they don't work in DEBUG mode. So I'm in a catch 22 - if I want to view the 404 page I have to turn off DEBUG, but then no static files are servers and I can't see any images, etc.
You can simply pretend you are in production. Run:
python manage.py collectstatic --noinput
To have all your files copied to STATIC_ROOT. Then, temporarily add the following to urls.py:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
You'll have to run collectstatic each time you make a change to any static files, so I would suggest live-editing in something like Firebug and then saving the finished product. Also, remember to delete the static directory and remove that line from urls.py when you're done.
I haven't tried it myself, but you might try setting DEBUG_PROPAGATE_EXCEPTIONS = True
https://docs.djangoproject.com/en/dev/ref/settings/