Django url pass through

92 views
Skip to first unread message

robert brook

unread,
Sep 22, 2014, 10:25:11 PM9/22/14
to django...@googlegroups.com
How is a url conf written so that if none of the useful urls are matched it will pass through to some sort of wild card regular expressions so that the view / redirection can be performed gracefully

Thanks

Daniel Rus Morales

unread,
Sep 23, 2014, 3:47:38 AM9/23/14
to django...@googlegroups.com
Hi Robert,

In those cases a HTTP 404 is the right response, to help Search Engines realise that such a URL doesn’t exist and to show users a "Page Not Found" message. To create a nice looking 404 response for users just write a view to handle it and add the “handler404” entry to your root URLConf module (the one near the settings.py module).

Adding something like this to urls.py:

handler404 = ‘views.http404_handler’

And creating that view in the corresponding views.py module:

from django.http import HttpResponseNotFound
from django.template import loader, RequestContext

def http404_handler(request):
    t = loader.get_template("404.html")
    return HttpResponseNotFound(
        t.render(RequestContext(request, {'request_path': request.path})))

Also create the template 404.html in your templates directory.


Good luck,
Daniel


On 23 Sep 2014, at 04:25, robert brook <software....@gmail.com> wrote:

How is a url conf written so that if none of the useful urls are matched it will pass through to some sort of wild card regular expressions so that the view / redirection can be performed gracefully

Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f981954c-679a-4000-9e4c-11d3e6ee3e09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

robert brook

unread,
Sep 23, 2014, 10:40:36 AM9/23/14
to django...@googlegroups.com
Thank you your response answers the "what do I do" as a response by calling the 404 view.

I also need to understand how the url will be coded so that an  invalid url that does not match anything falls through the the last url in the urls.py file.

I have a project urls.py file and expect that anything that does not match waitlist will fall through to the 2nd url.

url(r'^waitlist/', include('waitlist.urls')),
 url(r'^.*$', views.404_view),


If the project matches the value waitlist, the router will pull in the urls from the app.
Same question as above   

The url       my_domain.com/waitlist/   

will pull in the index view as expected

Will  the url       my_domain.com/waitlist/abc4#jjj/
pull in the 404_view and be handled graceffully?   

 url(r'^$', views.index, name='index'),
 url(r'^.*$', views.404_view),

Thanks  again

Collin Anderson

unread,
Sep 23, 2014, 2:48:09 PM9/23/14
to django...@googlegroups.com
It depends on what is defined in your waitlist.urls.
Reply all
Reply to author
Forward
0 new messages