[英]Issues with path() in urls.py in django 2.0.5
I am trying to do the following thing in urls.py but Django 2.0.5 doesn't seem to support url(). 我正在尝试在urls.py中执行以下操作,但是Django 2.0.5似乎不支持url()。 Instead of it, I used path() but still, its throwing invalid syntax error.
取而代之的是,我使用path(),但是仍然抛出了无效的语法错误。 Can someone give a clearer picture of path() as it seems to be not supporting regex.
有人可以给出更清晰的path()图片,因为它似乎不支持正则表达式。
Providing the code here: 在此处提供代码:
from django.contrib import admin
from django.urls import path
from .views import home_page
urlpatterns = [
path('$', home_page)
path('admin/', admin.site.urls),
]
You miss a ,
, and $
is unnecessary 您错过了
,
并且$
是不必要的
from django.contrib import admin
from django.urls import path
from .views import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
If you prefer to use url instead of path , that will work just fine. 如果您更喜欢使用url而不是path ,那就很好用了。 You just have to import from django.conf.urls instead.
您只需要从django.conf.urls导入即可。 So your import statement should look like this:
因此,您的导入语句应如下所示:
from django.conf.urls import url
Django says on their documentation page that this feature will likely be deprecated in future versions to re-path , however, url still works fine for me, and I'm running Django 2.0.7 ... so, I imagine it would work with yours as well. Django在其文档页面上说此功能可能会在以后的版本中不推荐使用,但可以重新使用url ,但是url对我来说仍然可以正常工作,并且我正在运行Django 2.0.7 ...因此,我想它可以与也是你的。 I guess because of this, with Django version 2 and above, it nows decides when it creates the boilerplate project that instead of importing urls from django.conf.urls , it imports path from django.urls .
我猜因此,在Django 2及更高版本中,它现在决定何时创建样板项目,而不是从django.conf.urls导入url,而是从django.urls导入路径 。 ( Note : PATH doesn't allow for regex)
( 注意 :PATH不允许使用正则表达式)
What I typically do, is create an app specific urls.py . 我通常要做的是创建特定于应用的 urls.py。 In that urls.py I'll import url from django.conf.urls and have my specific app level urls there:
在该urls.py中,我将从django.conf.urls导入url ,并在其中输入我特定的应用程序级别的url :
from django.conf.urls import url
from app_name import views # have to import views
urlpatterns = [
url(r'^$', views.index),
url(r'^users$',views.users),
]
Then in the project level urls.py I'll add the include module as so I can link it to my app specific urls.py file: 然后在项目级别 urls.py中添加include模块,以便可以将其链接到我的应用程序特定的 urls.py文件:
from django.conf.urls import url, include
from django.contrib import admin
from app_name import views
urlpatterns = [
url(r'^',include('app_name.urls')),
url(r'^admin/', admin.site.urls),
]
( Note : If you have a separate folder in between such that the folder structure looks something like mainproject>apps>app_name> (settings.py, views.py, admin.py etc...) you will have to create an __init__.py file in the apps folder as so Django can recognize the module. ( 注意 :如果您之间有一个单独的文件夹,使得文件夹结构看起来像mainproject> apps> app_name> (settings.py,views.py,admin.py等),则必须创建__init__。 py文件放在apps文件夹中,以便Django可以识别该模块。
Django2 has 2 functions for URLconfs, path() , re_path() .
Django2有2个用于URLconf的函数path()和re_path() 。
You can use regex paths (regular expression based paths) with re_path() , so remove $
and place ,
between two consecutive paths. 您可以使用re_path()正则表达式路径(基于正则表达式的路径),因此将其删除
$
和地点,
连续两个路径之间。
Note : Let suppose your app name is my_django_app created by python manage.py startapp my_django_app command.
注意 :假设让你的应用程序名称是Python的manage.py的startApp my_django_app命令创建my_django_app。
I created a new Django app named my_django_app and tried, it works fine. 我创建了一个名为my_django_app的新Django应用程序,并尝试了,它运行良好。 I have the following code in my urls.py file.
我的urls.py文件中包含以下代码。
"""my_django_proj URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from my_django_app.views import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
References : https://docs.djangoproject.com/en/2.0/topics/http/urls/
参考资料 : https : //docs.djangoproject.com/en/2.0/topics/http/urls/
django 2.0 - including urls file from an app to the root urls file
django 2.0-将应用程序中的urls文件包含到根urls文件中
Thanks. 谢谢。
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls)
]
As stated in the above answers, the $
is unnecessary while using path()
. 如以上答案所述,在使用
path()
时, $
是不必要的。 You are getting a syntax error due to the comma after admin.site.urls)
which should be removed. 由于
admin.site.urls)
之后的逗号引起语法错误,应将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.