1024programmer Java Explanation of path() and re_path() parameters of url routing in Django2.x

Explanation of path() and re_path() parameters of url routing in Django2.x

In the new version Django2.x, the routing of url is represented by path and re_path Instead, the module is imported by django1.xversion from django.conf.urls import url,include becomes the current Django2.x from django.urls import path, re_path, include

Look at the two pictures below:

  • Django1.10

Explanation of path() and re_path() parameters of url routing in Django2.x

Andtheurl()The function is passed four parameters, two of which are required:regexandview , and two optional: kwargs, and name. That is, regular expression and view are two required parameters.


  • Django2.1

Explanation of path() and re_path() parameters of url routing in Django2.x

Function path() has four parameters, two required parameters: route and view, two optional parameters: kwargs and name. That is, route and view are required parameters.

Then the main difference from the parameters of the old version is that url() is to write a regular expression (regex) route, while path() is to write a non-regular route (route). Let’s take a look at the meaning of the four parameters of the path() function.

1, path() parameter: route
     route is a criterion for matching URLs (similar to a regular expression).  When Django responds to a request, it starts with the first item in urlpatterns and matches items in the list in order until it finds a match.
     These guidelines will not match GET and POST parameters or domain names.  For example, when the URLconf handles the request https://www.example.com/myapp/, it will try to match myapp/.  When processing the request https://www.example.com/myapp/?page=3, it will only try to match myapp/.
 2, path() parameter: view
     When Django finds a matching criterion, it calls the specific view function and passes in an HttpRequest object as the first parameter. The "captured" parameters are passed in as keyword arguments.
 3, path() parameters: kwargs
     Any number of keyword arguments can be passed as a dictionary to the target view function.
 4, path() parameter: name
     Naming your URL allows you to uniquely reference it anywhere in Django, especially in templates.  This useful feature allows you to globally modify a URL pattern by changing just one file.  

Reference documentation: https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial01/


In Django2.1, the url writing method in django1.x began to be abandoned. In django2.x, there are two functions path and re_path

that describe url configuration.

path()

In the path introduced above, the first parameter route uses a common routing path that can be expressed by non-regular expressions.

Note:

  • To capture a value from a URL, use angle brackets.
  • The captured value can optionally include a converter type. For example, for Capture integer parameters. Matches if no converter/ is includedAny string other than characters.
  • There is no need to add the leading slash since every URL has one. For example, itarticlesis not/articles.

By default, the following path converters are available:

  • str– Matches any non-empty string except path separators'/' .This is the default value if the converter is not included in the expression.
  • int – matches zero or any positive integer. Returns a int.
  • slug – Matches any slug string consisting of ASCII letters or numbers, as well as hyphens and underscore characters. For example, building-your-1st-django-site.
  • uuid – Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example, 075194d3-6885-417e-a8a8-6c931e272f00. Returns a UUID instance.
  • path– Matches any non-empty string, including path separators '/'. This allows you to match the complete URL path, not just a part of the URL pathstr.

For example, if you want to match a function route in a view, the function has two formal parameters:

def peopleList(request,book_id)

The first request is the default, then the path automatically matches the second formal parameter of the function, matching format: , and returns a positive integer or zero value.

Explanation of path() and re_path() parameters of url routing in Django2.x

re_path()

If you encounter a URL pattern that neither path nor converter syntax is sufficient to define, then you need to use regular expressions. In this case, you need to use re_path() instead of path().

1 from django.urls import re_path

In Python regular expressions, the syntax for named regular expression groups is (?Ppattern), Groupname‘s name, and pattern is the pattern to be matched.

Let’s take the part in the circle above as an example. It can also be written using regular expressions. As follows:

1 re_path(r'^(\d+)/$',views.peopleList,name  ='peopleList'),

This can also match the formal parameters of the peopleList function in the views view.

So these two usage methods should be used according to the actual situation.


Additional additions:

In the secondary route (that is, in the urls file of the app), after urlpatterns, app_name=’app_name (your app name)’ should be added, otherwise an error will be reported.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/770322

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索