I'm following the Django tutorial. I've reached part 4, at the moment of building the vote form. Sadly, I cannot find the problem that is causing the following error:
NoReverseMatch at /polls/1/vote/
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P[0-9]+)/vote/$']
Both /polls/1/vote and /polls/1/ throw the error above.
My polls/urls.py
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/$', views.details, name='details'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
And here's the details.html, which shows the error at line 11, the form opening tag:
<h1>{{ question.question }}</h1>
{% if error %}
<p>
<strong>
{{ error }}
</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 }}</label>
<br/>
{% endfor %}
<input type="submit" value="Vote">
</form>
Related
I got an error, TemplateDoesNotExist at /polls/1/
I'm supposed to see the result of the vote after I vote.
Error is:
polls/details.html
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/1/
Django Version: 3.0.8
Exception Type: TemplateDoesNotExist
Exception Value:
polls/details.html
Exception Location: C:\Users\JohnDoe\Documents\JaneDoe\Django\trydjango\tutorial\lib\site-packages\django\template\loader.py in get_template, line 19
Python Executable: C:\Users\JohnDoe\Documents\JaneDoe\Django\trydjango\tutorial\Scripts\python.exe
Python Version: 3.8.4
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>
Templates are nested in a polls folder which is in a templates folder in the polls app.
I don't understand why I am getting an error in details.
How can I fix this?
Its a typo in your code. You need to change template_name="polls/details.html" to template_name="polls/detail.html" in your View.
Or, just rename the polls/detail.html file to polls/details.html.
I keep getting this when trying to load da page:
NoReverseMatch at /polls/
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']
In template C:\Users\sarah\Desktop\django2\myproject\my_site\polls\templates\polls\index.html, error at line 20
line 20:
<form action="{% url 'polls:vote' question.id %}" method="post">
I am a complete beginner at django, css, html, ... I kept checking in with the tutorial, comparing my code with the code reference in the tutorial, however I see no mistake.
My index.html:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>{{ question.question_text }}</li>
{% 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>
The vote function in views.py:
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):
# Redisplay the question voting form.
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,)))
urls.py:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
Thank you.
The main reason here is you are using your index.html with the form where you should really have this in polls/detail where you have access to a question object by pk. Right now you are trying to access the id property of question but question is undefined in your index.html.
Place the following in your polls/details.html
<h1>{{ question.question_text }}</h1>
<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>
Then, navigate to polls/1 or whatever question.id you want and then try and use the form.
The error references {% for film in form %} not being closed in: form-template.html:
{% for field in form %}
{{field.errors}}
<label>{{ field.label_tag }}</label>
{{ field }}
{$ endfor %}
My comment_form.html:
<form action="" method="post">
{$ csrf_token %}
{% include 'films/form-template.html' %}
<button type="submit">Submit</button>
</form>
urls.py:
app_name = 'films'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
# path('<int:film_id>/comment', views.add_comment, name='add_comment'),
path('<int:pk>', views.DetailView.as_view(), name='detail'),
path('<int:film_id>/add/$', views.CommentCreate.as_view(), name='add_comment'),
]
You have an error in the end tag of your for loop.
{$ endfor %}
Should be
{% endfor %}
working in django tutorials and this error in template rendering appears
NoReverseMatch at /music/1/ Reverse for 'favorite' with arguments
'(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
this is a detailed view for the problem
Error during template rendering
In template E:\Codes\bucky - Django Tutorials for Beginners\website\music\templates\music\detail.html, error at line 10
Reverse for 'favorite' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
1 <img src="{{ album.album_logo }}">
2
3 <h1>{{ album.album_title }}</h1>
4 <h2>{{ album.artist }}</h2>
5
6 {% if error_message %}
7 <p><strong>{{ error_message }}</strong></p>
8 {% endif %}
9
10 <form action="{% url 'music:favorite' album.id %}" method="post">
11 {% csrf_token %}
this is my music/urls.py
app_name = 'music'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<album_id>[0-9]+)/$', views.detail ,name='detail'),
url(r'^(?P<album_id>[0-9]+)/favorite/$', views.favorite ,name='favorite'),
]
the detail.html file where the error appear in the form
<img src="{{ album.album_logo }}">
<h1>{{ album.album_title }}</h1>
<h2>{{ album.artist }}</h2>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="{% url 'music:favorite' album.id %}" method="post">
{% csrf_token %}
{% for song in album.song_set.all %}
<input type="radio" id="song{{ forloop.counter }}" name="song" value="{{ song.id }}">
<label for="song{{ forloop.counter }}">
{{ song.song_title }}
{% if song.is_favorite %}
<img src="http://i.imgur.com/b9b13Rd.png">
{% endif %}
</label>
<br>
{% endfor %}
<input type="submit" value="Favorite">
</form>
so I started on the generic views section of the tutorial, until which point everything was smooth sailing, working perfectly and then I get this error:
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P[0-9]+)/vote/$']
Here is my urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name ='vote'),
]
And here is my views.py:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from django.http import Http404
from .models import Choice, Quesion
from django.core.urlresolvers import reverse
from django.views import generic
# Create your views here.
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
return Quesion.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Quesion
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Quesion
template_name = 'polls/results.html'
def vote(request, question_id):
p = get_object_or_404(Quesion, pk=question_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render(request, 'polls/detail.html', {
'question': p,
'error_message':"No choice selected",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results',args=(p.id,)))
And here is my 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_txt }} </label><br />
{% endfor %}
<input type="submit" value="vote" />
</form>
You have two problems. The first is that DetailView doesn't provide a question variable in the template, but provides one called object instead. So all instances of question in the template needs to be changed to object.
Secondly, the URL is expecting a keyword argument question_id, but you're passing a non-keyword argument to the {% url %} tag. You need to change that to say question_id=object.id. Your detail.html should look like this:
<h1>{{ object.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question_id=object.id %}" method="post">
{% csrf_token %}
{% for choice in object.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_txt }} </label><br />
{% endfor %}
<input type="submit" value="vote" />
</form>