2

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 :)
Alasdair
  • 298,606
  • 55
  • 578
  • 516
Edgar Navasardyan
  • 4,261
  • 8
  • 58
  • 121
  • 1
    Take a look at [this](https://docs.djangoproject.com/en/2.0/topics/http/views/#customizing-error-views) – Sachin Dec 04 '18 at 11:31
  • 1
    Users landing here in future, if we redirect using the `handler404`, it may stop redirecting the i18n urls. – Ravi Ojha Mar 05 '19 at 10:53

1 Answers1

4

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • could you please be kind enough to confirm that an appropriate way to check whether this is an api call or not would be `request.content_type == 'application/json` – Edgar Navasardyan Dec 04 '18 at 11:56
  • It really depends on your API. GET requests don't have a body, so they won't have a content-type header. You might also need to consider whether your API clients send the correct headers in practice. – Alasdair Dec 04 '18 at 12:14