Reputation: 129
I have just started learning Django and want to know the reason behind a particular behavior the Django's polls application is showing.
Everything works fine according to the below code but when I remove that '/' from the end of 'detail' URL path, I can access the details page of all questions except for the primary key(pk) 1. In case of pk 1, I am getting a 404 Error. It would be great if I will get an explanation. Thanks! Cheers!
Here are the code files:
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
polls/views.py
.......
.......
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/details.html', {'question': question})
.......
.......
polls/templates/details.html
<h1> {{ question.question_text }} </h1>
<ul>
{% for choice in question.choice_set.all %}
<li> {{ choice.choice_text }} </li>
{% endfor %}
</ul>
Upvotes: 0
Views: 50
Reputation: 1875
There is a setting that controls this, APPEND_SLASH
which defaults to True
. That's probably why it was working initially. Once you've accessed this URL without the slash, your browser will cache the redirect, and will try to go the URL with slash when accessing the URL without checking the server again.
You can disable this browser behaviour in Chrome, by going to devtools, click on the 3 vertical dots next to "close" button that reads "customize and control devtools" on hover. In the menu, select settings and find the "Network" section. You need to check the "Disable cache (while devtools is open)" checkbox.
When enabling this Browser setting, if you retry with devtools open, you should not have the redirect anymore and hopefully it should work.
Upvotes: 1