pythondjangodjango-urlsurlconf

Django - redirecting to home page if no url matched


Basically, my goal is to redirect the user to the view that renders index.html if nothing from URLConf matched the requested url. Then, on the clientside, I would manage routing as appropriate or show 404 page.

So basically, the question is,

  1. how to implement this redirecting in Django? I have tried this:

    url(r'^(.*)$', views.index),
    

    ...some other urls

...and not surprisingly, (.*) means that ALL urls are redirected to index.html, no matter if there it matches any other URL or not. So should I just place this in the very end of the url list? I have a feeling that I should use some sort of middleware, or maybe Django has a native way to address this issue?

  1. An umpleasant side-effect of this would be for users of my rest app. Each time they all an invalid URL, they would get HTML (that would in effect be the HTML for home.html) instead of json or 404 error. Well, nothing ugly about index.html code, and yet it sounds like a bad idea. So would I separate this logic so that the users of REST app get 404 error instead of being redirected to index.html? This is much about coding patterns, not Django itself. And yet I would really appreciate any comments on this as well :)

Solution

  • You can add the catch all at the end of your URL config, but it leads to some unexpected behaviour (e.g. redirects from /my-page to /my-page/ (appending slash) no longer work).

    A better approach would be to write a custom middleware. You could look at the redirect middleware for inspiration since it handles redirects.

    If you don't want to redirect API requests, you could add a check to your middleware. For example you could not redirect requests where the URL starts with /api/, or the request has an Accept: application/json header.

    Another option would be to use a custom 404 handler that renders your index page. Again, in the handler you have the request object, so you can treat API requests differently.