我在Django 教程(第 4 部分)中,并尝试创建一个允许用户选择投票答案的表单。问题正确加载,但是当我单击“投票”(即选择选项并提交表单)时,以下错误不断显示:
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/polls/vote/6
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^$ [name='index']
^polls/ ^(?P<poll_id>\d+)/$ [name='detail']
^polls/ ^(?P<poll_id>\d+)/results/$ [name='results']
^polls/ ^(?P<poll_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, polls/vote/6, didn't match any of these.
以下是 detail.html 中的代码,其形式为:
{{ poll.question }}
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/polls/vote/{{ poll.id }} " method="post">
{% csrf_token %}
{% for choice in poll.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>
我怀疑问题出在这条线上<form action="/polls/vote/{{ poll.id }} " method="post">
,但我不知道如何解决它。