问题:对于Django 2.0,在urls.py中使用path()或url()更好吗?

在django在线类中,讲师让我们使用该url()函数调用视图并使用urlpatterns列表中的正则表达式。我在YouTube上看到了其他示例。例如

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

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


#and in polls/urls.py

urlpatterns = [        
    url(r'^$', views.index, name="index"),
]

但是,在阅读Django教程时,他们path()改用例如:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name="index"),        
]

此外,正则表达式似乎不适用于该path()函数,因为使用path(r'^$', views.index, name="index")将找不到mysite.com/polls/视图。

使用path()没有正则表达式匹配的正确方法是正确的吗?是url()更强大,但更复杂,所以他们正在使用path()与开始我们吗?还是针对不同工作使用不同工具的情况?

In a django online course, the instructor has us use the url() function to call views and utilize regular expressions in the urlpatterns list. I’ve seen other examples on youtube of this. e.g.

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

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


#and in polls/urls.py

urlpatterns = [        
    url(r'^$', views.index, name="index"),
]

However, in going through the Django tutorial, they use path() instead e.g.:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name="index"),        
]

Furthermore regular expressions don’t seem to work with the path() function as using a path(r'^$', views.index, name="index") won’t find the mysite.com/polls/ view.

Is using path() without regex matching the proper way going forward? Is url() more powerful but more complicated so they’re using path() to start us out with? Or is it a case of different tools for different jobs?


回答 0

从Django文档获取url

url(regex, view, kwargs=None, name=None)此函数是的别名django.urls.re_path()。在将来的版本中可能不推荐使用。

和之间的主要区别path使用不带正则表达式的路由

您可以re_path用于复杂的正则表达式调用,也可以仅path用于更简单的查找

From Django documentation for url

url(regex, view, kwargs=None, name=None) This function is an alias to django.urls.re_path(). It’s likely to be deprecated in a future release.

Key difference between and is that path uses route without regex

You can use re_path for complex regex calls and use just path for simpler lookups


回答 1

django.urls.path()功能允许使用更简单,更易读的URL路由语法。例如,此示例来自先前的Django版本:

url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive)

可以写成:

path('articles/<int:year>/', views.year_archive)

django.conf.urls.url() 以前版本中的功能现在可以作为django.urls.re_path()。保留旧位置是为了向后兼容,而不会很快淘汰。django.conf.urls.include()现在django.urls可以从导入旧功能,因此您可以使用:

from django.urls import include, path, re_path

URLconfs中。进一步阅读django doc

The new django.urls.path() function allows a simpler, more readable URL routing syntax. For example, this example from previous Django releases:

url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive)

could be written as:

path('articles/<int:year>/', views.year_archive)

The django.conf.urls.url() function from previous versions is now available as django.urls.re_path(). The old location remains for backwards compatibility, without an imminent deprecation. The old django.conf.urls.include() function is now importable from django.urls so you can use:

from django.urls import include, path, re_path

in the URLconfs. For further reading django doc


回答 2

pathDjango 2.0只是几周前才发布的新功能。大多数教程都不会针对新语法进行更新。

当然,这应该是一种更简单的处理方式。我不会说URL更强大,但是您应该能够以任何一种格式表示模式。

path is simply new in Django 2.0, which was only released a couple of weeks ago. Most tutorials won’t have been updated for the new syntax.

It was certainly supposed to be a simpler way of doing things; I wouldn’t say that URL is more powerful though, you should be able to express patterns in either format.


回答 3

正则表达式似乎不适用于path()具有以下参数的函数:path(r'^$', views.index, name="index")

应该是这样的:path('', views.index, name="index")

第一个参数必须为空才能输入正则表达式。

Regular expressions don’t seem to work with the path() function with the following arguments: path(r'^$', views.index, name="index").

It should be like this: path('', views.index, name="index").

The 1st argument must be blank to enter a regular expression.


回答 4

Path是Django 2.0的新功能。在这里解释:https : //docs.djangoproject.com/en/2.0/releases/2.0/#whats-new-2-0

看起来更像pythonic方式,并允许在传递给视图的参数中不使用正则表达式…例如,您可以使用int()函数。

Path is a new feature of Django 2.0. Explained here : https://docs.djangoproject.com/en/2.0/releases/2.0/#whats-new-2-0

Look like more pythonic way, and enable to not use regular expression in argument you pass to view… you can ue int() function for exemple.


回答 5

从v2.0开始,许多用户正在使用path,但是我们可以使用path或url。例如,在django 2.1.1中,可以通过url映射到函数

from django.contrib import admin
from django.urls import path

from django.contrib.auth import login
from posts.views import post_home
from django.conf.urls import url

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^posts/$', post_home, name='post_home'),

]

其中posts是应用程序,而post_home是views.py中的函数

From v2.0 many users are using path, but we can use either path or url. For example in django 2.1.1 mapping to functions through url can be done as follows

from django.contrib import admin
from django.urls import path

from django.contrib.auth import login
from posts.views import post_home
from django.conf.urls import url

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^posts/$', post_home, name='post_home'),

]

where posts is an application & post_home is a function in views.py


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。