# URLs without a trailing slash don't redirect

**URL:** https://forum.djangoproject.com/t/urls-without-a-trailing-slash-dont-redirect/14585
**Category:** Mystery Errors
**Created:** [July 2, 2022, 6:32am UTC](https://forum.djangoproject.com/t/urls-without-a-trailing-slash-dont-redirect/14585 "2022-07-02T06:32:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mohitey7](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mohitey7/32/9110_2.png) [@mohitey7](https://forum.djangoproject.com/u/mohitey7)
#### Post date: [July 2, 2022, 6:32am UTC](https://forum.djangoproject.com/t/urls-without-a-trailing-slash-dont-redirect/14585/1 "2022-07-02T06:32:34Z")

</div>

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](http://127.0.0.1:8000/login) doesn’t open and throws an error where as [http://127.0.0.1:8000/login/](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’),  
]

 ![Error](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/a/a79b4d630be2128f25562341b643edbf67140075.jpeg)

---

<div class="post-metadata">

### Author: ![AhmdMWddh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ahmdmwddh/32/21707_2.png) [@AhmdMWddh](https://forum.djangoproject.com/u/AhmdMWddh)
#### Post date: [July 2, 2022, 11:24am UTC](https://forum.djangoproject.com/t/urls-without-a-trailing-slash-dont-redirect/14585/2 "2022-07-02T11:24:10Z")

</div>

> [@mohitey7](#):
>
> urlpatterns = [  
> path(‘admin’, admin.site.urls),  
> path(‘’, include(‘application.urls’)),  
> ]

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

```auto
path(‘admin/’, admin.site.urls),
path(‘/’, include(‘application.urls’)),

```

> [@mohitey7](#):
>
> 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’),  
> ]

```auto
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’),
]

```

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [July 2, 2022, 11:32am UTC](https://forum.djangoproject.com/t/urls-without-a-trailing-slash-dont-redirect/14585/3 "2022-07-02T11:32:15Z")

</div>

See the [`APPEND_SLASH`](https://docs.djangoproject.com/en/4.0/ref/settings/#append-slash) setting as one way to resolve this.

---

<div class="post-metadata">

### Author: ![mohitey7](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mohitey7/32/9110_2.png) [@mohitey7](https://forum.djangoproject.com/u/mohitey7)
#### Post date: [July 4, 2022, 6:49am UTC](https://forum.djangoproject.com/t/urls-without-a-trailing-slash-dont-redirect/14585/4 "2022-07-04T06:49:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [July 4, 2022, 12:03pm UTC](https://forum.djangoproject.com/t/urls-without-a-trailing-slash-dont-redirect/14585/5 "2022-07-04T12:03:19Z")

</div>

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`](https://docs.djangoproject.com/en/4.0/ref/settings/#std-setting-APPEND_SLASH) setting is only used if [`CommonMiddleware`](https://docs.djangoproject.com/en/4.0/ref/middleware/#django.middleware.common.CommonMiddleware) is installed (see [Middleware](https://docs.djangoproject.com/en/4.0/topics/http/middleware/)).

Do you have `CommonMiddleware` installed?

Also, you have:

> [@mohitey7](#):
>
> ```auto
> urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
> urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
> 
> ```

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/`.

---

<div class="post-metadata">

### Author: ![mohitey7](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mohitey7/32/9110_2.png) [@mohitey7](https://forum.djangoproject.com/u/mohitey7)
#### Post date: [July 5, 2022, 5:21am UTC](https://forum.djangoproject.com/t/urls-without-a-trailing-slash-dont-redirect/14585/6 "2022-07-05T05:21:55Z")

</div>

@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 🙂
