【问题标题】:Unable to get django declared app routes working as pages无法让 django 声明的应用程序路由作为页面工作
【发布时间】:2019-06-15 17:31:53
【问题描述】:

我正在创建一个 Django 应用程序,但遇到了无法识别路线的问题。这是我尝试创建的同一个 DJango 投票应用程序,但文档代码不起作用。下面是我的代码:

djangoproject/urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^simpleapp/', include('simpleapp.urls')),
    url(r'^admin/', admin.site.urls),
]

simpleapp/views.py

from django.shortcuts import render
from django.http import HttpResponse, request


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

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)

simpleapp/urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    # ex: /simpleapp/
    # url('', views.index, name='index'),
    # ex: /simpleapp/5/
    url('<int:question_id>/', views.detail, name='detail'),
    # ex: /simpleapp/5/results/
    url('<int:question_id>/results/', views.results, name='results'),
    # ex: /simpleapp/5/vote/
    url('<int:question_id>/vote/', views.vote, name='vote'),
]

如果我取消注释simpleapp/urls.py代码的''路径的第一个url,所有显示的页面都是''路径。但是,如果我保留 url '' 路径注释,那么路由会给我以下错误:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/simpleapp/34/
Using the URLconf defined in simple_django.urls, Django tried these URL patterns, in this order:

^simpleapp/ <int:question_id>/ [name='detail']
^simpleapp/ <int:question_id>/results/ [name='results']
^simpleapp/ <int:question_id>/vote/ [name='vote']
^admin/
The current path, simpleapp/34/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

我无法使用django.conf.urlsdjango.urls 导入path()url() 成功。我正在使用 python 3.6.7 版和 Django 2.1.5 版。我错过了什么?

【问题讨论】:

    标签: python django url path routes


    【解决方案1】:

    您可以定义 Django 1.x

    之类的路由
    url(r'^simpleapp/', include('simpleapp.urls')),
    url(r'^admin/', admin.site.urls),
    

    这是 Django 1.x 类型的路由定义

    在 Django 2 中,您必须像这样定义路由:

    from django.contrib import admin
    from django.urls import include, path
    
    urlpatterns = [
        path('polls/', include('polls.urls')),
        path('admin/', admin.site.urls),
    ]
    

    在 Django 中 2 个路由定义没有正则表达式

    请参阅文档以了解 Django 2

    中的路由定义

    【讨论】:

    • 谢谢。 I was unable to get the path() imported using django.conf.urls or django.urls. url() was successful. I am using python version 3.6.7 and Django version 2.1.5. What am I missing?docs.djangoproject.com/en/2.1/intro/tutorial03的文档@
    • 酷。删除正则表达式不起作用。我无法让from django.urls import include, path 工作。我确实得到了url
    【解决方案2】:

    您在 simpleapp/urls.py 中使用的是路径语法,而不是正则表达式。将其更改为:

    urlpatterns = [
      path('<int:question_id>/', views.detail, name='detail'),
      path('<int:question_id>/results/', views.results, name='results'),
      path('<int:question_id>/vote/', views.vote, name='vote'),
    ]
    

    您的项目 URL 正在使用正则表达式,因此您不应更改它们,尽管您可能希望使它们保持一致。

    【讨论】:

    • 谢谢。 I was unable to get the path() imported using django.conf.urls or django.urls. url() was successful. I am using python version 3.6.7 and Django version 2.1.5.我错过了什么? docs.djangoproject.com/en/2.1/intro/tutorial03 上的文档我尝试删除正则表达式,但没有成功。
    • 我非常怀疑您使用的是 2.1。如果from django.urls import path 不起作用,那么您可能使用的是 1.11 或更低版本。
    • Daniel,添加了 django 和 python 版本的图像。是同一个版本
    • 但这仅表明您已经为 Python 3 安装了 Django。您实际上是在使用 Python 3 来运行您的网站吗?例如,您的 urls.py 顶部的样板注释仅提及 url(),而您使用 2.1 获得的注释仅提及 path()
    • 好吧。让我尝试卸载并重新安装 django。给我一点时间
    猜你喜欢
    • 2020-02-08
    • 2016-12-25
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2019-04-16
    • 2017-08-04
    • 1970-01-01
    相关资源
    最近更新 更多