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,
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?
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.