Forums

404 pages in Django.

Here is what I have done to use a custom 404 page.

In urls.py,

    urlpatterns = patterns('',
            url(r'^$', views.home, name='home'),
            url(r'^blog/(?P<post>\d)$', views.blog, name='blog'),
            url(r'^blog$', views.blog),
            url(r'^.+$', views._404),
                )

What this does is this, django will try to match the requested url with the first three patterns and on failing will always match with the last pattern, thanks to the regex, and will load the 404.html template. Now I know that the response will have a "OK 200" code instead of 404 but how much of an issue is this? Will this raise any complications or security holes? Thanks again.

People do this much more than you may imagine. Of course this most often seems to be on retail sites where they want to try and sell you something no matter what address you land on. As long as it isn't an issue to you it shouldn't be an issue.

I'm sure there are others who will have arguments to the contrary. We'll just have to see if they choose to respond.

~a2j

Thanks for the reply.

@vivekannan: You are most welcome. Also, it's good to have you here. How did you discover PA?

@a2j I don't exactly remember how I found PA, but I was looking for a browser based IDE for python and I think DuckDuckGo led me here.

Edit: Now I remember. Someone put up a link to PA on Hacker News and that's how I found it.

I can't think of any serious complications or security holes from returning a 200 instead of a 404 status code. But an alternative way that might be better is to create a Django template called 404.html. If you do that, then anything that makes Django 404 will return that template, and the status code will be correct too.

@giles Okay, so I changed it according to the docs with a proper handler_404 and everything. Thanks again.

No problem, glad to help!

@vivekannan: I recently discovered DDG as well. I like it as an alternative to the GooglePlex!! Especially the ability to use "Anonymous Cloud" for your account settings.

@PA: Speaking of account settings. DDG allows us to set our own colors (colours for ya'll). Have you thought of letting logged in users turn the site dark? I way prefer a black bg. +1 for me if it's on the list...☺

~a2j

@a2j -- you mean for the regular web pages? Yikes. I guess we could do that... But have you considered a user style sheet plugin? Then you can make any site look like you want.

@giles: Sounds like it's not on your RADAR, so if I'm the only one, np. But, yes I was thinking site as a whole (The PA content anyway). No, I really hadn't thought of a browser plug-in. I just may look into it now though...☺

~a2j

Hello everybody, I have my custom 404 page but it doesn't launch when I type a bad URL, the page who launch is the 404 from another app instead, but in my debeloment server it is launched and i have exclty same project in both places, I hope somebody can help me. my URL file:

from django.conf.urls import patterns, include, url from django.contrib import admin from django.conf import settings from django.conf.urls.static import static

urlpatterns = patterns('', # Examples: # url(r'^$', 'footcare.views.home', name='home'), # url(r'^blog/', include('blog.urls')),

url(r'^admin/', include(admin.site.urls)),
url(r'^', include("headtoe.urls", namespace="headtoe")),
url(r'^weblog/', include('zinnia.urls', namespace='zinnia')),
url(r'^comments/', include('django_comments.urls')),
url(r'^search/', include('zinnia.urls.search', namespace='zinnia')),
url(r'^ckeditor/', include('ckeditor.urls'))

)

handler404 = 'headtoe.views.my_custom_page_not_found_view'

What other app is showing the 404? It's probably just getting to the 404 before the handler that you're showing us.

Hello thanks for pay attention to my trouble, so I m getting the 404 from the app called zinnia and it is after the app i want launch its 404 called headtoe but i have same app in my local machine and in this I'm using the development serve who come with Django 1.7, so there are any diference. Thanks

If you're getting it from zinnia, then that is somehow overriding your setting or matching the request when you're not expecting it to. Have a look at the zinnia urls.

I'm understanding your point of view but my headache is i have the same app in my local machine and it works, I mean my custome 404 is launched. Why here in pythonanywhere that is not happening like this? I think the trouble is about the path to my view who lunch my 404 in some way it si diferent here in the hosting.

The assertion that something is "the same app" on a different machine is almost never true. The environment is different, the settings are different, the versions of apps installed are usually different etc. etc. etc. You need to debug something in the environment that it is running in.

no no im using virtualenv and and I installed all there using the requirement.txt file so all is same there i have in my virtualenv in my local machine but is ok i will check everything again, thanks for your coperations.