[Resolved] Using tutorial. Issues with view.py

I’m using PyCharm Pro (latest version). The code works correctly using 127.0.0.1:8000/polls/ and shows me each question in the dbase in the index.view. But when I select the question and the detail.view is executed I get this url, polls/polls/1/ as shown below. I’m completely stumped as to the why the index view works and the detail view doesn’t. I get the same message manually correcting the URL path to 127.0.0.1:8000/polls/1/ with the same result.

Request Method: GET Request URL: http://127.0.0.1:8000/polls/polls/1/ Using the URLconf defined in innotest.urls, Django tried these URL patterns, in this order:

admin/
polls/ [name=‘index’]
polls/ int:question_id/ [name=‘detail’]
polls/ int:question_id/vote/ [name=‘vote’]
polls/ int:question_id/results/ [name=‘results’]
The current path, polls/polls/1/, didn’t match any of these.

Note: line spacing is compressed. (Actual code conforms to PEP 8)
<views.py>

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from .models import Question
# Create your views here.

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    # template = loader.get_template("polls/index.html")
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

def results(request, question_id):
    response = "You're looking at the results of %s" % question_id
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

<polls/url.py>

app_name = 'polls'
urlpatterns = [
        path("", views.index, name="index"),
        path("int:question_id/", views.detail, name="detail"),
        path("int:question_id/vote/", views.vote, name="vote"),
        path("int:question_id/results/", views.results, name="results"),
        ]

I already don’t have any hair up top, so I’m doubly frustrated attempting to pull my hair out.

Thanks in advance for pointing out my fubar. I’m assuming since the default view works the other project files are correct at the top level.

Side note: When you’re posting code, templates, or HTML here, enclose it between lines of three backtick - ` characters to preserve formatting and to prevent special characters from being interpreted by the forum software. This means you’ll have a line of ``` before your code, then your code, then another line of ```.

Please edit your post to add those lines before and after each file.

Done!

No idea how I missed the formatting bar and that I could manually use Markdown.

It should be copacetic now but I can do better don’t hesitate to say so. I know I’m as ignorant as a rock but it can be fixed by learning whatever topic my ignorance is displayed.

Doug

No problem - I can’t count how many times I’ve pointed this out to people.

Anyway, I think we’ll also need to see your index.html template. (That’s the one with the incorrect link, right?)

I think the problem is with your urlpatterns. If you look closely at the path instances after index, you’ll see int:question_id, but those aren’t wrapped with angle brackets. Thus, Django is looking for a URL that is literally like /polls/int:question_id/.

Once you include the angle brackets, Django should capture the information as a variable to pass to your view. A fixed up example would look like:

path("<int:question_id>/", views.detail, name="detail"),

Those parts of the URL in the path definition are called “converters.” I discuss converters and URLs in depth if you’d like another point of view in my Understand Django article on URLs.

Hopefully once you fix up your paths, you’ll be on your way! Good luck!

That’s one of the two problems - it does address his second issue regarding why it fails when he manually corrects the URL. I think we still need to see the template to try to understand why the page is trying to direct him to /polls/polls/1/ instead of /polls/1/.

I’ve corrected the urls.py. @mblayman thank you.
But the /polls/polls/1/ error still exists.

@KenWhitesell
Templates below

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>/vote/", views.vote, name="vote"),
    path("<int:question_id>/results/", views.results, name="results"),
    ]

Note: Template paths ‘/mysite/polls/templates/polls’
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Voting Index</title>
</head>
<body>
{% if latest_question_list %}
    <ul>
        {% for question in latest_question_list %}
        <li>
    <a href="polls/{{ question.id }}/">{{ question.question_text }}</a>
        </li>
        {% endfor %}
    </ul>
{% else %}
<p>
    No polls are available.
</p>
{% endif %}
</body>
</html>

details.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Detail</title>
</head>
<body>
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
</body>
</html>

@mblayman
The link you sent is appreciated, but it the examples are missing the brackets as @KenWhitesell pointed out. Perhaps you haven’t updated it to Django 3?

# project/urls.py
from django.urls import path

from application import views

urlpatterns = [
    path("", views.home),
    path("about/", views.about),
    path("contact/", views.contact),
    path("terms/", views.terms),
]

In that portion of the example, those paths aren’t using converters. Not every URL is required to have a converter. Later in the article shows some examples that use converters.

In your template, it looks like you have a hardcoded path to the views. I don’t recall the tutorial steps and maybe it’s introduced later, but Django templates can use the url template tag to build the correct URL.

That’s going to look like:

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

As for your specific problem in its current form, your browser is on the polls index page when this is rendered (e.g., localhost:8000/polls/). Since the current template uses <a href="polls/{{ question.id }}/">, it is using a relative path (you can observe that because there is no leading slash). A relative path means that the link looks like /polls/polls/1/ from your browser’s point of view (by adding /polls/ and polls/1/ together).

If the tutorial is going to introduce the url template tag later, you could fix your link by putting a slash in front of polls like:

<a href="/polls/{{ question.id }}/">

@mblayman

I found the error in the index template right after I replied to @KenWhitesell . As described it was a change from

<a href="polls/{{ question.id }}/">{{ question.question_text }}</a>

To

<a href="/polls/{{ question.id }}/">{{ question.question_text }}</a>

As for the for the url function, the tutorial at this point is leading up to the function.

I’d had so much trouble as desktop support with hard coded values that when I finally learned to code I never used absolute values and stored then externally so it could be changed by support or the Help Desk from the program documentation.

But a hearty yes on the url function, it’s awesome. I appreciate the features I am seeing in the framework. It’s obvious it is well thought out to eliminate the need to have absolute variables anywhere in the code.

Great! I’m glad to hear you got over your hurdles.