Django URL without trailing slash not working
Asked Answered
D

4

8

Updated Page not found (404)

This is a silly problem. I just created a project and have been trying to figure out this problem.

from django.conf.urls import url
from django.views.generic import TemplateView

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name="index.html")),
    url(r'^about$', TemplateView.as_view(template_name="about.html")),
    url(r'^contact$', TemplateView.as_view(template_name="contact.html"), name="contact"),
    url(r'^test$', TemplateView.as_view(template_name="test_start"), name="test_start"),
    url(r'^test/sample$', TemplateView.as_view(template_name="test_start"), name="test_start"),
]

is included into

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('frontend.urls'))
]

When I go to localhost:8000/about, I get redirected to localhost:8000/about/ and there I get 404 Not Found.

UPDATE: I added more URLs into my URLconf.

UPDATE 2: I meant to not include trailing slashes. My apologies.

UPDATE 3: I opened the same URL in Firefox and the URL works like I intend. Could this be a problem with redirection and browser cache?

Diecious answered 12/3, 2016 at 23:4 Comment(2)
Your config looks fine. From which browser/Rest Client are you testing?Imitative
I am using templates from another project. I'll take a look at the templates to see if I can figure anything out. And the browser is Google Chrome.Diecious
U
2

First, I found out that Chrome automatically adds trailing slash at the end of the URL

Trailing URL Slashes in Django

So if you don't have a trailing slash on your URLS, a 404 redirect will show if you're using Chrome, but not if, say, Firefox.

Then from the comment of knbk from here,

How Django adds trailing slash

I made sure I had the CommonMiddleware class in setting.py and added 'APPEND_SLASH = False'

Then, cleared Chrome's cache, and problem solved!

Ulmer answered 22/12, 2020 at 3:54 Comment(2)
helpfull!!..ThanksHenriettahenriette
this is actually explained why the setting never worked because it's not he server problem it's a browser default settings!Beachlamar
C
1

Did you enable the append_slash setting ? https://docs.djangoproject.com/en/dev/ref/settings/#append-slash

using this may help to make it more explicit and is recommended throughout the django tutorials

url(r'^about/$', TemplateView.as_view(template_name="about.html")),

EDIT:

Deactivate the APPEND_SLASH settings (False) and use

url(r'^about$', TemplateView.as_view(template_name="about.html")),
Custumal answered 12/3, 2016 at 23:21 Comment(1)
Sorry, the slashes in the end of the patterns are typos. I actually want to remove the trailing slashes.Diecious
F
0

You can just remove the $ from your regex, this indicates the end of line

url(r'^about', TemplateView.as_view(template_name="about.html")),

You could also just include a slash to your regex, since Django has a APPEND_SLASH setting which will issue a redirect

url(r'^about/$', TemplateView.as_view(template_name="about.html")),

if the request URL does not match any of the patterns in the URLconf and it doesn’t end in a slash, an HTTP redirect is issued to the same URL with a slash appended.

Francium answered 12/3, 2016 at 23:17 Comment(0)
R
-1

Change your url pattern for "about" to:

url(r'^about/?$', TemplateView.as_view(template_name="about.html")),

Without the /?, the regex ^about$ matches a string containing exactly the word "about".

Radius answered 12/3, 2016 at 23:16 Comment(1)
Sadly, for some reason this does not help.Diecious

© 2022 - 2024 — McMap. All rights reserved.