我的姜戈不起作用(我正在学习教程)



我对django很陌生。所以我正在使用教程网站学习。 我认为在现场打字完全一样,但这不是工作。 所以请给我建议。

我的网站/网址.py

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

mysite/polls/urls.py

from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),   #url : base/polls/
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'),
]

mysite/polls/templates/polls/detail.html

<h1>{{ question.question_text }}</h1>
{% if errer_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>

在mysite/polls/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()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

错误

NoReverseMatch at /polls/1/
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/polls/1/
Django Version: 3.0.7
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']
Exception Location: C:Usershdh45crawling_practicefirstPvenvlibsite-packagesdjangourlsresolvers.py in _reverse_with_prefix, line 677
Python Executable:  C:Usershdh45crawling_practicefirstPvenvScriptspython.exe
Python Version: 3.8.3
Error during template rendering
In template X:pythonpractice_wellpollstemplatespollsdetail.html, error at line 5

我想投票网址不起作用。 也许有什么问题。 但我不知道确切的安瑟。我的谷歌搜索能力是无用的。帮帮我。

<form action="{% url '***polls***:vote' question.id %}" method="post">

民意调查是一个字符串,你的 urls.py 在一个path('**<int:question_id>**/vote/', views.vote, name='vote')<int:question_id>是一个int.我认为这是一个错误

我也是乞丐

最新更新