Krya Krya
Krya Krya

Reputation: 23

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

NoReverseMatch at /polls/

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

index.html:

    {% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
    <!-- # the 'name' value as called by the  url  template tag -->
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
<!-- or:
    <li><a href=" url 'detail' question.id "> question.question_text </a></li>
    How does one make it so that Django knows which app view to create for a url when using the  url  template tag?
    So we use polls:detail
 -->
    {% endfor %}
    </ul>
    {% else %}
        <p>No polls are available.</p>
    {% endif %}

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

enter image description hereenter image description here

Below it's console error. Other relative questions in stackoverflow have answer like: not question_id! It's question.id!

error at line 123 Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P[0-9]+)/vote/$']:

113         {% endfor %}
114         </ul>
115         {% else %}
116             <p>No polls are available.</p>
117         {% endif %}
118     
119         <h1>{{ question.question_text }}</h1>
120     
121         {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
122     
123         <form action="{% url 'polls:vote' question.id %}" method="post">
124         {% csrf_token %}
125         {% for choice in question.choice_set.all %}
126             <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
127             <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
128         {% endfor %}
129         <input type="submit" value="Vote">
130         </form>
131         </content>

Upvotes: 2

Views: 2814

Answers (5)

error

class DetailView(generic.DetailView):
    model= Questions
    template_name ="polls/detail.html"
    
class ResultView(generic.DetailView):
    model= Questions
    template_name ="polls/results.html"

solution:

class DetailView(generic.DetailView):
    model= Questions
    context_object_name = "question" 
    template_name ="polls/detail.html"
    
class ResultView(generic.DetailView):
    model= Questions
    context_object_name = "question"
    template_name ="polls/results.html"

Upvotes: 1

gzcarlosrd
gzcarlosrd

Reputation: 101

This error is caused by adding the form tag in the index.html and not in the detail.html.

Just change the html content of the form to the detail.html template.

Upvotes: 0

shivam prasad
shivam prasad

Reputation: 1

Replying late, but found it to be a comman typo while working out the Django docs. I am 99% sure that you named your model Questions in place of Question as django detail_view reads model name in lowercase, your template in causing reverse look up error.

Upvotes: 0

keerthi
keerthi

Reputation: 21

Change ListView class DetailView(generic.ListView): to class DetailView(generic.DetailView):

this may work

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599638

You haven't got a variable called question at that point of the template. It only exists within your for loop, but the error is happening in the form tag which is after the end of that loop.

The url tag is the only one that will actually show an error, because it needs to use the value of question.id to create a URL; but in fact all the other uses of the variable, such as question.question_text, would also display as blank.

I don't quite know why you've structured your template like this, but I suspect everything from the h1 onwards should be much higher, before the endfor tag.

Upvotes: 2

Related Questions