Django Tutorial Part 4 problems with slash at end of URL

When I go to 127.0.0.1:8000/polls I click on the What’s New? I get the following error:

|Request URL:|http://127.0.0.1:8000/polls/1//|

Using the URLconf defined in mysite.urls , Django tried these URL patterns, in this order:

  1. polls/ [name=‘index’]
  2. polls/ < int:question_id >/ [name=‘detail’]
  3. polls/ < int:question_id >/results/ [name=‘results’]
  4. polls/ < int:question_id >/vote/ [name=‘vote’]
  5. admin/

The current path, polls/1// , didn’t match any of these.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False , and Django will display a standard 404 page.

My urls.py file looks like this:

from django.urls import path
from . import views

app_name = ‘polls’

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’)
]

I know that Django adds the trailing ‘/’ but why? And why doesn’t tutorial account for this? I never checked Part 3. But let me know if there is anything else, I can post to show you guys.

And thanks!

What do you refer by this?

Also, Part 3 of the tutorial has the same urls as you posted and says:

Take a look in your browser, at “/polls/34/”. It’ll run the detail() method and display whatever ID you provide in the URL. Try “/polls/34/results/” and “/polls/34/vote/” too – these will display the placeholder results and voting pages.

1 Like

Yes, but you are entering the URL and it will either add or not based on if you enter http:// 127.0.0.1:8000/polls/1 or http:// 127.0.0.1:8000/polls/1/.

Look at the URL as it is when you are at http ://127.0.0.1:8000/polls it will take you to http ://127.0.0.1:8000/polls/1/ but when you click the anchor the URL is then malformed and it becomes http ://127.0.0.1 :8000/polls/1// that second trailing slash is causing the error.

If you are just putting in the URL as expected everything works but that is not the point. Sometimes you need to go through the motions. Why can’t Django see that the URL pointed to by What’s Up? anchor already has the trailing slash, and it shouldn’t be added. I can get to it if I enter the URL directly but not by clicking on it. Why?

We’ll need to see the template where this is being rendered. My guess would be that you’ve got an extra slash in there somewhere.

And regarding this question:

First, I’m not sure what you mean by Django adding a slash. If there’s an extra slash in the url, it’s because you’re supplying it.

(Now, when a url is invoked, Django can add a slash if a url doesn’t match at first - but that’s done when the url is invoked, not when it’s rendered in a template.)

1 Like

I completely agree but take a look at the second line of my original question, that second slash is part of the error message that I’m getting back when I click on the anchor. If I have hover over the anchor it shows http:// 127.0.0.1:8000/polls/1/ but when it is clicked it becomes http:// 127.0.0.1:8000/polls/1// this is what is happening. I understand what Django is supposed to be doing but it’s not working in that manner and I’m resolving with an extra / that is not put there by me I’m just clicking a link.

Again, please review my entire question, the error page is first displayed and then my urls.py file to show the board that is what the Tutorial states I should show.

We need to look more deeply into this. I understand what you’re seeing when you hover the link. Now let’s look under the covers to see why that is happening.

That’s why we need to see the template rendering the page, and the corresponding snippet of the html as rendered and shown in your browser’s developer tools.

1 Like

Okay, my bad. My brain dropped the ball when you asked for the template file. So I pulled it the index.html and found this:

< li>< a href="{% url 'polls:detail' question.id %}/">{{ question.question_text }}< /a >< /li>

which there is ‘/’ after the question.id and so I removed it to reflect this:

< li>< a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}< /a >< /li>

and it works now. It was me and apologize for repeating myself to everyone helping.

Thanks so much and I’ll do better to take a look at the template code to make sure it is what it should be.

No worries! Glad to see you’ve got it resolved.

1 Like

Thank you! I had the following problem: For example, when I am at the address /1/ and with a button I go to another address, which should be / question /, but instead I go to http://127.0.0.1:8000/1/question/ and raize an 404 error. Your answer reminded me to look at the template for the address written on the button and it turned out that I wrote href=“question/”, and it had to be href="/question/". Thank you very much!

1 Like