我正在从官方 django 教程中学习 django。当我从表单中投票时,我收到了这个错误。这可能是由 views.py 下的投票功能引起的
这是我的views.py/投票功能:
def vote(request,poll_id):
p=get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render_to_response('polls/detail.html', {'poll':p,
'error_message' : "didint select anything ",}, context_instance= RequestContext(request))
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))
这是错误消息屏幕:
**ValueError 在 /polls/2/vote/
以 10 为底的 int() 的无效文字:'on'**
请求方式:POST 请求网址:127.0.0.1:8000/polls/2/vote/
Django 版本:1.4 异常类型:ValueError 异常值:int() 的无效文字,基数为 10:'on' 异常位置:/usr/local/lib/python2.7/dist-packages/django/db/models/fields/ get_prep_value 中的init .py,第 537 行
这是我的 polls/urls.py :
from django.conf.urls import patterns, include, url
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$','detail'),
url(r'^(?P<poll_id>\d+)/results/$','results'),
url(r'^(?P<poll_id>\d+)/vote/$','vote'),
)
这是 project/urls.py :
from django.conf.urls import patterns, include, url
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$','detail'),
url(r'^(?P<poll_id>\d+)/results/$','results'),
url(r'^(?P<poll_id>\d+)/vote/$','vote'),
)