gpt4 book ai didi

python - 按照官方 Django 教程并制作模板后,我遇到了 int 转换问题

转载 作者:行者123 更新时间:2023-11-28 21:09:57 25 4
gpt4 key购买 nike

<分区>

我正在看 Django 教程:https://docs.djangoproject.com/en/1.9/intro/tutorial03/#a-shortcut-render我被困住了。

目前,当我尝试转到 127.0.0.1:8000/polls/{ any arbitrary number }/时,Django 会抛出一个 ValueError 因为它无法转换 '{ num }/'。

这是确切的错误:

ValueError at /polls/1/

invalid literal for int() with base 10: '1/'

这是 detail.html 文件:

{[ question }}

这是详细函数 polls/views.py:

def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist.")
return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id):
response = HttpResponse("You are looking at the results of question %s.")

这是 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<question_id>[0-9]+/$)', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

为什么 Django 试图将 '1/' 转换为 int?如果您需要查看更多文件,这就是整个项目:https://github.com/wOstensen/my-new-blog

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com