Django URL without trailing slash not working

Magnus Teekivi picture Magnus Teekivi · Mar 13, 2016 · Viewed 7.7k times · Source

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?

Answer

maazza picture maazza · Mar 13, 2016

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")),