[英]Django URL Pattern Syntax - raw string
What is the difference between:有什么区别:
path('index/', views...
And和
path(r'^index/$', views...
I use the first example but I see everyone using the raw string syntax in examples I look up.我使用第一个示例,但在我查找的示例中,我看到每个人都使用原始字符串语法。 Are there any differences between the way Django handles the two examples?
Django 处理这两个示例的方式有什么不同吗?
The path
method is the full match method , so when your path will be exactly as described, you will have the possibility to handle the request with the view that you've described. path
方法是完全匹配方法,因此当您的路径与描述的完全一致时,您将有可能使用您描述的视图处理请求。
The re_path
allows you more control with regular expressions. re_path
允许您使用正则表达式进行更多控制。 Let's extend your example r"^index/?$"
, here "?"让我们扩展您的示例
r"^index/?$"
,这里是 "?" sign allows the user to use the trailing slash "/" at the end of the URL or not use.符号允许用户在 URL 末尾使用尾部斜杠“/”或不使用。
You can play with regexp here: https://regex101.com你可以在这里玩正则表达式: https ://regex101.com
The Django documentation about re_path is here: https://docs.djangoproject.com/en/dev/topics/http/urls/#using-regular-expressions关于 re_path 的 Django 文档在这里: https ://docs.djangoproject.com/en/dev/topics/http/urls/#using-regular-expressions
The business needs could be very complex and maybe you won't be able to make a match for a needed URL format with just the path
features, but in 99% this option will be enough.业务需求可能非常复杂,您可能无法仅使用
path
功能来匹配所需的 URL 格式,但在 99% 的情况下,此选项就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.