Reverse for 'results' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/results/$']

63 Views Asked by At

I just got to the generic views section of the Django tutorial on their website, but I'm stuck at this part and keep getting this error. This question has been answeared before, but for an older version of the tutorial and Django. I was hoping someone here could help me. Here is the code:

urls.py

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

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_id' : question_id})

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/detail.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,)))

detail.html

<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>

results.html

<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:results' question.id %}">Vote again?</a>

there is a specific message pointing to the last line of results.hmtl

In template C:\Users\Erenildo\Desktop 2\Python\Django\DjangoTut\Djanguiho\mysite\polls\templates\polls\results.html, error at line 9

edit1: models.py

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text
2

There are 2 best solutions below

0
On BEST ANSWER

The problem is your results view. You should include question in the template context, not question_id.

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

Try just doing this for results.py

<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 'results' question_id=question.id %}">Vote again?</a>

So long as you only have one URL patterns with the namespace results then you should be fine.