【问题标题】:Django tutorial error part3Django 教程错误第 3 部分
【发布时间】: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

标签: python django


【解决方案1】:

通过网址,我发现您没有正确提及您的网址。当您通过命令提示符使用python manage.py runserver 启动服务器时,您可以看到一系列输出语句,其中一个是您在http://127.0.0.1:8000/ 的启动开发服务器,您添加您的预期URL 在您的情况下应该是http://127.0.0.1:8000/polls/34/ 将运行您已连接到的方法。

注意:在您的情况下,您的内部服务器可能不同。

【讨论】:

  • 感谢您的帮助,感谢您的帮助,我能够解决问题!非常感谢!!!
  • 很高兴能帮上忙!
【解决方案2】:

在您的图片中,您转到polls/34,但您还需要输入地址 如果你没事就从django tutorial访问

http://localhost:8000/polls/34/

【讨论】:

    【解决方案3】:

    在您应该提供的网址中

    http://127.0.0.1:8000/polls/1/

    可见的 o/p 是“您正在查看问题 1。”

    http://127.0.0.1:8000/polls/34/

    可见的 o/p 是“您正在查看第 34 题。”

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 1970-01-01
      • 2016-12-24
      • 2017-08-21
      • 2014-04-06
      • 2017-11-15
      • 1970-01-01
      • 2017-07-16
      • 2021-01-22
      相关资源
      最近更新 更多