Polls Tutorial Not Found Error

Hi,

I can’t view any of the changes I’ve made in the Python files, whenever I click on a link I get ‘The requested resource was not found on this server.’

The only two links I can get to are localhost:8000/polls and localhost:8000/admin and localhost:8000/polls/1/results - but I can’t get to the index or detail or vote page.

Thanks for any suggestions.

Lisa

We’re going to need to see the files you’re using, specifically your project level and application level urls.py files, your view files, and maybe the template files you’re trying to load.

When you post them, be sure to surround the code blocks in ``` so it’s easy to read on the forum :slight_smile:

1 Like

project level urls.py:

from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

App level urls:

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'),
]

views.py:

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.urls import reverse

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    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):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/results.html', {'question': question})

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'polls/details.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

templates - detail (not working):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Details</title>
    </head>
    <body>
        <h1>{{ question.question_text }}</h1>

        {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

        <form action="{% url 'polls:vote' question.id %}" method="post">
            {% csrf_token %}
            {% for choice in question.choice_set.all %}
                <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
                <label for="choice {{ forloop.counter }}">
                    {{ choice.choice_text }}
                </label>
                <br>
            {% endfor &}
            <input type="submit" value="Vote">
        </form>
    </body>
</html>

templates - results (working):

 <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Polls App</title>
    </head>
    <body>
        <h1>{{ question.question_text }}</h1>

        <ul>
            {% for choice in question.choice_set.all %}
            <li>
                {{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}
            </li>
            {% endfor %}
        </ul>

        <a href="{% url 'polls:detail' question.id %">Vote again?</a>
    </body>
</html>

I’m in the middle of page 4, where we’re about to change the redundant bits of code.

At a minimum, you appear to be missing the close brace in the statement above - it should be:
<a href="{% url 'polls:detail' question.id %}">

1 Like

You have a syntax error in the above, it should be:
{% endfor %}

1 Like

When you’re trying to mark off chunks of code, make sure you’re using the backtick - ` and not the apostrophe - it looks like you’ve got some lines of ‘’’, rather than ```.

Also, those lines of ``` need to be lines of their own. They shouldn’t be on a line with any other text.

1 Like

Thanks for your help Ken. I’ve fixed the syntax errors in my code, still nothing.

Can you be more specific as to which links on which page are failing? It would help me focus in on the right section of your code.

Failing:
http://localhost:8000/index/ , …polls/1/detail/ , …/vote/

Working:
http://localhost:8000/polls/ ,… /1/results/ and also /admin/

I’m not sure if this is actually going to cause a problem, but you have a space between the word choice and the two open-braces in the line below.

Ok, so I don’t see any url entry for /index/, so I wouldn’t expect that to work.

Your index page would just be /polls/.

Likewise, there’s no entry for a url /detail/, it’s just /polls/1/.

Finally, there’s no url entry for /vote/, it would be something like /polls/1/vote/

When you’re defining a name for a url, that’s an “internal-to-the-server” name, not part of the url visible to the user. The url is constructed only from the components identified in the first parameter.

Which file are we talking about?

Ok, so I don’t see any url entry for /index/ , so I wouldn’t expect that to work.

Polls urls.py:

path('', views.index, name='index'),

Likewise, there’s no entry for a url /detail/, it’s just /polls/1/.

This is definitively what the tutorial said to do, surprised me too. I changed it, still nothing.

Finally, there’s no url entry for /vote/ , it would be something like /polls/1/vote/

Even when I try to put in /polls/1/vote nothing comes up. Sometimes I get a 505 error but I haven’t figured out when and why.

Also I don’t understand why the polls/1/results/ page does work.

Edit - the detail page now works too… Really confused by this.

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

This is what I’m referring to when I said:

So let’s look at this a little more closely.

You have:

path('', views.index, name='index'),

That’s 3 parameters you’re passing to the path function:

  1. An empty string - that’s the url you use to access this view
  2. The name of the view. (The index method in the views module - views.py)
  3. name=‘index’ - This is an internal name for this path, not a part of the url.

Additionally, since you have the line:

In your root urls file, this means that every url in your polls app will start with polls/

So, since all urls in the polls app starts with polls/, then the full path polls/1/detail/ matches the above defined url. It’s your specification of detail/ in that first parameter that provides the definition, not the name='detail' parameter.

Correct - your urls entry is:

Which shows a trailing / after the word vote.
Using /polls/1/vote isn’t going to work because you didn’t supply the trailing /.

We’ve covered a lot of ground here - which is good, If you still have questions or are still seeing something, please feel free to continue asking here.

Ken

So I’ve gone through the whole tutorial again and corrected various typos. It now works.

Using /polls/1/vote isn’t going to work because you didn’t supply the trailing / .

This was especially helpful, I made that mistake or similar a couple of times.

Thanks for all your help.

1 Like