1

I am trying to route all unknown URLs to a single view. However, in doing this my known URLs only work when they do not have a trailing slash, despite setting APPEND_SLASH to True in "settings.py".

Here is some code:

settings.py:

APPEND_SLASH = True
ADMIN_URL = "admin/"

urls.py:

from django.conf import settings
from django.contrib import admin
from django.urls import include, path, re_path
from myapp.views import my_catch_all_view

urlpatterns = [
  path(settings.ADMIN_URL, admin.site.urls),
  re_path(r"^.*", my_catch_all_view),
]

If I go to "localhost:8000", I correctly get routed to my catch-all view.

And if I go to "localhost:8000/foobar/", I correctly get routed to my catch-all view.

And if I go to "localhost:8000/admin/", I correctly get routed to the admin view.

But, if I go to "localhost:8000/admin", I incorrectly get routed to my catch-all view.

I have seen this answer, but unfortunately that doesn't work for me.

Any suggestions?

Alasdair
  • 298,606
  • 55
  • 578
  • 516
trubliphone
  • 4,132
  • 3
  • 42
  • 66
  • try `r'^(?P.*)$')` – tushortz Oct 29 '18 at 09:19
  • @Tushortz - Thanks. That has no effect. – trubliphone Oct 29 '18 at 09:29
  • 1
    How about `re_path(r"^.\/*", my_catch_all_view),` we catch it with `slash` at the end only. If it doesn't have `/` it will get redirected there? – Đào Minh Hạt Oct 29 '18 at 10:22
  • *I have seen this answer, but unfortunately that doesn't work for me.* - what behavior do you see with that answer? Why doesn't it work? – Alasdair Oct 29 '18 at 10:33
  • There are a couple of other approaches you could consider - set [`handler404`](https://docs.djangoproject.com/en/2.1/topics/http/views/#customizing-error-views) to your catch-all view (note this may not be suitable if you want a different 404 page for views that raise `Http404`). Or you might be able to subclass `CommonMiddleware` and call your catch-all view there. – Alasdair Oct 29 '18 at 10:34
  • @Alasdair - I don't know why it doesn't work. The behavior I see with that answer is that "admin/" goes to the admin view and "admin" goes to my_catch_all_view. – trubliphone Oct 29 '18 at 10:54
  • @Alasdair - the `handler404` solution sounds promising. If I can't figure out another way, I will use that. Thanks. – trubliphone Oct 29 '18 at 10:55
  • If your catch-all regex is `r'^.*/$'` as in that answer, then `admin` shouldn't go to the catch all url. `admin` won't match the catch-all, so you should be redirected to `admin/`, which will be matched by the admin URL pattern before the catch-all. – Alasdair Oct 29 '18 at 11:00
  • @Alasdair - That no longer works for "localhost:8000" (no path). I can get around that by adding `path("", my_catch_all_view)` to urlpatterns. But that seems _icky_. – trubliphone Oct 29 '18 at 11:29
  • True, you would need need to handle the no path case if you used that answer. But that's a bit different from saying "it doesn't work for me" in your original question, or that it causes "admin" to redirect to my_catch_all_view in your comment. – Alasdair Oct 29 '18 at 12:18

0 Answers0