我正在关注一个教程,我一直在收到这些错误!为什么?我需要你的帮助。在错误页面上它显示“NoReverseMatch at / polls / 使用参数“(1,)”找不到“详细信息”。尝试过1种模式:['民意调查/ $(?P [0-9] +)/ $']“ 请帮忙,我正在努力学习本教程。教程链接“https://docs.djangoproject.com/en/1.11/intro/tutorial04/”
我的index.html
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li> <a href="{% url 'polls:detail' question.id %}">{{question.question_text}}</a></li>
{% endfor %}
</ul>
{% else %}
<p> No polls are available </p>
{% endif %}
urls.py in polls app
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.detail, name='detail'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
我在投票应用中的views.py
from django.template import loader
from django.http import HttpResponse
from .models import Question
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
# Create your views here.
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', {'question': question})
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>
答案 0 :(得分:0)
您是否在根模块中包含了polls.urls的urls.py?
例:
urlpatterns = [
url(r'^polls/', include('polls.urls')),
]