Python Django 2.0 路径错误 ?: (2_0.W001) 的路由包含 '(?P<', 以 '^' 开头,或以 '$' 结尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47661536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:18:14  来源:igfitidea点击:

Django 2.0 path error ?: (2_0.W001) has a route that contains '(?P<', begins with a '^', or ends with a '$'

pythondjangopython-3.xdjango-viewsdjango-urls

提问by Joe Tynan

I'm new to Django and am trying to create the back end code for a music application on my website.

我是 Django 的新手,正在尝试为我网站上的音乐应用程序创建后端代码。

I have created the correct view in my views.py file (in the correct directory) as shown below:

我在我的 views.py 文件(在正确的目录中)中创建了正确的视图,如下所示:

def detail(request, album_id):
    return HttpResponse("<h1>Details for Album ID:" + str(album_id) + "</h1>")

however, when creating the url or path for this (shown below)

但是,在为此创建 url 或路径时(如下所示)

#/music/71/ (pk)
path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),

I am experiencing a warning on my terminal stating:

我在终端上遇到警告,说明:

?: (2_0.W001) Your URL pattern '^(?P<album_id>[0-9])/$' [name='detail'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().

and whenever the /music/(for which the path works) is followed by a number, such as /music/1(which is what I want to be able to do) the page cannot be found and the terminal gives the above warning.

并且每当/music/(路径适用的)后跟一个数字时,例如/music/1(这是我想要能够做的)无法找到页面并且终端会发出上述警告。

It may be a simple error and just me being stupid but I'm new to Django and python regex statements, so any help is appreciated.

这可能是一个简单的错误,只是我很愚蠢,但我是 Django 和 python regex 语句的新手,因此我们不胜感激。

回答by Alasdair

The new path()syntax in Django 2.0 does not use regular expressions. You want something like:

path()Django 2.0 中的新语法不使用正则表达式。你想要这样的东西:

path('<int:album_id>/', views.detail, name='detail'),

If you want to use a regular expression, you can use re_path().

如果要使用正则表达式,可以使用re_path().

re_path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),

The old url()still works and is now an alias to re_path, but it is likely to be deprecated in future.

旧的url()仍然有效,现在是 的别名re_path,但将来可能会被弃用。

url(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),

回答by Stryker

Just to add to what @alasdair mentioned, I added re_path as part of the include and it works fine. Here is an example

只是为了添加@alasdair 提到的内容,我添加了 re_path 作为包含的一部分,它工作正常。这是一个例子

Add re_path to your import (for django 2.0)

将 re_path 添加到您的导入中(对于 django 2.0)

from django.urls import path, re_path

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^$', home, name='home'),

]

回答by Akash Gupta

Instead of using 're_path' you can also use ''(empty string) as the first argument of your path(). I have used it and it worked for me.

除了使用 're_path',您还可以使用 ''(空字符串)作为 path() 的第一个参数。我用过它,它对我有用。

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.index,name='index'),
]

回答by DILIP

Use an empty string '' instead of '/' or r'^$'. It works like a charm. Code is as below:

使用空字符串 '' 而不是 '/' 或 r'^$'。它就像一个魅力。代码如下:

from django.urls import path, re_path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home, name='home'),
]

回答by Hamza Naji

If it doesn't work add this code to yoursite\urls.py inside urlpatterns:

如果它不起作用,请将此代码添加到 urlpatterns 中的 yoursite\urls.py:

path('music/<int:album_id>/', views.detail, name="detail"),

回答by Parvez Khan Pathan

In django 2.0 version primary key write this way...

在 django 2.0 版本主键这样写...

urls.py

网址.py

from django.urls import path

from . import views


urlpatterns = [
    path('', views.course_list),
    path('<int:pk>/', views.course_detail),
]

回答by Purushotam Sangroula

url() is deprecated in newer version of django. So instead of using url use re_path() in your urls file as follows:

url() 在较新版本的 django 中已弃用。因此,在您的 urls 文件中使用 re_path() 而不是使用 url,如下所示:

from django.urls import path, re_path
from . import views

urlpatterns = [
    #url(r'^(?P<album_id>[0-9]+)/$', views.detail, name='detail'),
    path('', views.index, name='index'),
    re_path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),
]