URLs without a trailing slash don't redirect

It’s been 10 days of learning Django. I am building a project and have integrated quite number of templates now. However, since yesterday, I encountered an error due to non-addition of slash post the URL. The URL doesn’t reroute for e.g. http://127.0.0.1:8000/login doesn’t open and throws an error where as http://127.0.0.1:8000/login/ opens without an error. I have come across several solutions but they don’t work.

At the start of the project, it worked. But as soon as the complexity of my project grew, the above error came up.

Below are my URL patterns for project.

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
path(‘admin’, admin.site.urls),
path(‘’, include(‘application.urls’)),
]

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


URL Patterns for my application

from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
path(‘’, views.home, name=‘home’),
path(‘login/’, views.login, name=‘login’),
path(‘register/’, views.register, name=‘register’),
path(‘logout/’, views.logout, name=‘logout’),
path(‘doctor/’, views.doctor, name=‘doctor’),
path(‘patient/’, views.patient, name=‘patient’),
]

you can try for project urls.py and remove each trailing backslash / after each url in application urls.py

path(‘admin/’, admin.site.urls),
path(‘/’, include(‘application.urls’)),
urlpatterns = [
path(‘’, views.home, name=‘home’),
path(‘login’, views.login, name=‘login’),
path(‘register’, views.register, name=‘register’),
path(‘logout’, views.logout, name=‘logout’),
path(‘doctor’, views.doctor, name=‘doctor’),
path(‘patient’, views.patient, name=‘patient’),
]

See the APPEND_SLASH setting as one way to resolve this.

Can you elaborate. I have already gone through this answer but couldn’t understand what it stated. It mentions that by default it is true. What if I wanted an URL to route automatically /.

E.g. Login to Login/ by default

First, a side note. When posting code here, enclose each block of code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted, which is critical with Python.

The last sentence in that doc specifies that:

The APPEND_SLASH setting is only used if CommonMiddleware is installed (see Middleware).

Do you have CommonMiddleware installed?

Also, you have:

What are these settings? (MEDIA_URL, MEDIA_ROOT, STATIC_URL, STATIC_ROOT)

If you notice that error message carefully, you’ll see that the error is raised by django.views.static.serve, and that it is matching a “match anything pattern” at the end of your URLs. This is being added to your URL list by one of those two additions to urlpatterns. You should ensure that MEDIA and STATIC files aren’t based off of your root url, but are instead based on their own path such as media/ and static/.

2 Likes

@KenWhitesell Thank you so much. Made an error in settings file wherein I missed to define (MEDIA_URL, MEDIA_ROOT, STATIC_URL, STATIC_ROOT) and had directly mentioned the path in project URL. Works a charm now :slight_smile: