堆栈溢出有三个问题,错误略有不同。我的论证也正在通过,但我似乎没有弄清楚错误是什么?
NoReverseMatch at /polls/1/vote/
Reverse for 'result' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: POST
Request URL: http://localhost:8000/polls/1/vote/
Django Version: 1.8.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'result' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: C:\Python27\Lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496
Python Executable: C:\Python27\python.exe
这是我的民意调查/ views.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Movie, Rating
# Create your views here.
def detail(request, movie_id):
try:
movie = get_object_or_404(Movie,pk = movie_id)
except Movie.DoesNotExist:
raise Http404("Movie not found")
return render(request, 'polls/detail.html', {'movie': movie})
def result(request, movie_id):
movie = get_object_or_404(Movie, pk = movie_id)
return render(request, 'polls/result.html', {'movie': movie})
def vote(request, movie_id):
p = get_object_or_404(Movie, pk = movie_id)
try:
selected_rating = p.rating_set.get(pk=request.POST['rating'])
except(KeyError, Rating.DoesNotExist):
return render(request, 'polls/detail.html', {'movie':p, 'error_message':"you didn't select a rating",})
else:
selected_rating.votes += 1
selected_rating.save()
return HttpResponseRedirect(reverse('polls:result', args=(p.id,)))
请问我是否需要任何其他文件,我会把它们放在这里。
这是detail.html
<h1> {{movie.movie_text}}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' movie.id %}" method="post">
{% csrf_token %}
{% for rating in movie.rating_set.all %}
<input type="radio" name="rating" id="rating{{ forloop.counter }}" value="{{ rating.id }}" />
<label for="rating{{ forloop.counter }}">{{ rating.rating_number }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
这里是result.html
<h1>{{movie.movie_text}}</h1>
<ul>
{% for rating in movie.rating_set.all%}
<li>{{rating.rating_text}} -- {{rating.votes }} vote{{rating.votes|pluralize}}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' movie.id %}">Vote Again? </a>
这是polls / urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
#ex: /polls/
url(r'^$', views.index, name = 'index'),
#ex /polls/5/
url(r'^(?P<movie_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<movie_id>[0-9]+)/result/$', views.result, name='results'),
url(r'^(?P<movie_id>[0-9]+)/vote/$', views.vote, name='vote')
]
这是mysite / urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls', namespace='polls')),
]
答案 0 :(得分:1)
你没有一个名为“polls:result”的视图,你有一个名为“polls:results”的视图。在polls / urls.py中更改名称,或在reverse
视图中调用vote
时使用的值。