django urls without a trailing slash show Page not found

Viewed 278

in django urls without a trailing slash in the end i get this result "Page not found 404" the same project and the same code in one pc i get different result. this code is when i get the page without slash:

from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about', views.about, name='about'),
]

and this the same code but i should to add slash

from django.urls import path
from . import views

urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
]

what i am waiting in the frontend its my views.py

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def about(request):
return HttpResponse('about page')

i am waiting for your help guys

1 Answers

This behaviour is controlled by the APPEND_SLASH setting. In the default setting APPEND_SLASH is set to True which means if the requested URL does not match any paths set in urls.py an HTTP redirect will issue to the same URL with a trailing slash.

Eg: Assume a request to /foo.com/bar will redirect to /foo.com/bar/ in case the provided URL /foo.com/bar is an invalid URL pattern.

Documentation states that

Note that the redirect may cause any data submitted in a POST request to be lost.

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

please read the documentation for further clarification.

Related