【发布时间】:2018-11-13 10:12:25
【问题描述】:
enter image description here 我有一个问题,我正在编写 Django 教程,我想在 /polls/34/ 的浏览器中使用它,但我不知道该怎么做 当我尝试放入浏览器时,它会从 localhost 拒绝
我使用 python3 和 django 2.0
Django 教程给出了这个 在浏览器中查看“/polls/34/”。它将运行 detail() 方法并显示您在 URL 中提供的任何 ID。也可以试试“/polls/34/results/”和“/polls/34/vote/”——它们会显示占位符结果和投票页面
但我无法加载它
请建议并检查以下代码
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
mysite/polls/views.py
from django.http import HttpResponse
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
【问题讨论】:
-
你能显示完整的回溯吗,你有什么错误
-
我刚刚在正文中附上了一张图片
-
我输入了 localhost/polls/34 但浏览器响应如下 Not Found The requested URL /polls/34/ was not found on this serve