New features of urls changes in django2.0

Django2.0 has changed a lot compared to 1.xx

To summarize:
1. Examples:

from django.urls import path
from . import views

urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    path('articles/<int:year>/', views.year_archive),
    path('articles/<int:year>/<int:month>/', views.month_archive),
    path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]

The web services are all requested through the url of the browser
and then written
in url.py by the background routing system of django processed in the background.

Notice:

  • To capture the value in a url, you need to use angle brackets instead of the previous round brackets;
  • The captured value can be converted to a specified type, such as int in the example. By default, the captured result is saved as a string type, excluding the special character /;
  • There is no need to add / at the beginning of the matching pattern, because by default, each url has a leading /. Since everyone has a part, there is no need to waste time writing a special one.

Match example:

  • /articles/2005/03/ will match the third article and call views.month_archive(request, year=2005, month=3);
  • /articles/2003/ matches the first article and calls views.special_case_2003(request);
  • /articles/2003 will not match one, because it is missing a slash at the end, and all patterns in the list end with a slash;
  • /articles/2003/03/building-a-django-site/ will match the last one and call views.article_detail(request, year=2003, month=3, slug="building-a-django-site"

2. Path Converter
By default, Django has the following built-in path converters:

  • str: matches any non-empty string, but does not contain slashes /, if you do not specify a converter, this is used by default;
  • int: matches 0 and positive integers, returns an int type
  • slug: It can be understood as concepts such as comments, suffixes, and attachments. It is an explanatory character that is dragged at the end of the url. This converter matches any ASCII character as well as hyphens and underscores, like 'building-your-1st-django-site';
  • uuid: Matches an object in uuid format. To prevent conflicts, it is stipulated that dashes must be used and all letters must be lowercase, eg '075194d3-6885-417e-a8a8-6c931e272f00'. returns a UUID object;
  • path: matches any non-empty string, the point is that the path separator '/' can be included. This converter can help you match entire urls instead of url strings.

3. Although the url of Django 2.0 using regular expressions
has been changed to 'configuration', it is still compatible with the old version. The compatible method is to use the re_path() method instead of the path() method . The re_path() method is basically the same as the previous url() method, but the imported location has changed. The following is an example, comparing the syntax of the Django1.11 era, what is the difference?

from django.urls import path, re_path

from . import views

urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    re_path('articles/(?P<year>[0-9]{4})/', views.year_archive),
    re_path('articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/', views.month_archive),
    re_path('articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<slug>[\w-_]+)/', views.article_detail),
]

It is different from the path() method in two points:

  • Year does not match non-four-digit numbers such as 10000, which is determined by regular expressions
  • All parameters passed to the view are of type string. Unlike the path() method, you can specify the conversion to a certain type. Be careful when receiving parameters in views

5. Summary
Except for the above parts, other knowledge points of Django2.0 routing system are basically the same as those of Django1.11, that is, there is a little difference in the way of writing.

You just need to import the re_path() method correctly using from django.urls import re_path and replace the url() method with it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324515201&siteId=291194637