Python 对于 django 2.0,在 urls.py 中使用 path() 或 url() 更好吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47947673/
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:26:48  来源:igfitidea点击:

Is it better to use path() or url() in urls.py for django 2.0?

pythondjangodjango-urls

提问by FrostedCookies

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.

在 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"),
]

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

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

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.

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

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?

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

回答by iklinac

From Django documentation for url

来自 Django 文档的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.

url(regex, view, kwargs=None, name=None)此函数是 的别名django.urls.re_path()。它可能会在未来的版本中被弃用。

Key difference between pathand re_pathis that pathuses route without regex

path和之间的主要区别re_pathpath使用没有正则表达式的路由

You can use re_pathfor complex regex calls and use just pathfor simpler lookups

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

回答by Sylvernus Akubo

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

django.urls.path()函数允许使用更简单、更易读的 URL 路由语法。例如,以前的 Django 版本中的这个例子:

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.urlsso you can use:

django.conf.urls.url()以前版本的功能现在可以作为django.urls.re_path(). 旧位置保留为向后兼容,没有即将弃用。旧django.conf.urls.include()函数现在django.urls可以从中导入,因此您可以使用:

from django.urls import include, path, re_path

in the URLconfs. For further reading django doc

URLconfs 中。进一步阅读Django 文档

回答by Daniel Roseman

pathis 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.

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

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.

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

回答by Danish Shaikh

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

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

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

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

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

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

回答by Denis Cottin

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

路径是 Django 2.0 的一个新特性。在这里解释: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.

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

回答by Nani Chintha

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

从 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'),

]

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

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