Django 2.0URL New Configuration Introduction

Source: Internet
Author: User
Tags uuid

Instance

Let's look at an example:

 fromDjango.urlsImportPath from.ImportViewsurlpatterns=[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),]

Attention:

    1. To capture a value in a URL, you need to use angle brackets instead of the previous parentheses;
    2. You can convert a captured value to a specified type, such as an int in an example. By default, the captured result is saved as a string type and does not contain / this special character;
    3. The top of the matching pattern does not need to be added / , because by default, each URL takes one of the front / , and since everyone has a part, there is no time to waste special writing.

Matching examples:

    • /ARTICLES/2005/03/will match the third one and call views.month_archive (Request, year=2005, month=3);
    • /ARTICLES/2003/matches the first and calls views.special_case_2003 (request);
    • /articles/2003 does not match one, because it has a trailing slash, and all the 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 "
Path Converter

By default, Django has the following path converters built into it:

    • STR: matches any non-empty string, but does not contain slashes / , if you do not specifically specify the converter, then this is used by default;
    • int: matches 0 and positive integers, returns an int type
    • Slug: Can be understood as annotations, suffixes, attachments, etc., is the URL dragged in the last part of the explanatory character. The converter matches any ASCII characters as well as connectors and underscores, such as ' building-your-1st-django-site ';
    • UUID: Matches an object in a UUID format. In order to prevent conflicts, a dash must be used, and all letters must be lowercase, such as ' 075194d3-6885-417e-a8a8-6c931e272f00 '. Returns a UUID object;
    • Path: Matches any non-empty string, with emphasis on the ability to include the path delimiter '/'. This converter can help you match the entire URL rather than a paragraph of the URL string.
Customizing the Path Converter

is to write a class and include the following members and attributes:

    • Class Property Regex: A regular Expression property in the form of a string;
    • To_python (self, Value) method: One used to convert the matched string to the data type you want, and pass it to the view function. If the conversion fails, it must eject the valueerror exception;
    • To_url (self, Value) method: Converts a Python data type to a URL method, with the reverse operation of the method above.

For example, create a new converters.py file, the same directory as urlconf, and write the following class:

class Fourdigityearconverter:     ' [0-9]{4} '    def To_python (self, value):         return int (value)     def To_url (self, value):         return ' %04d ' % value

After writing the class, register it in urlconf and use it, as shown below, to register a yyyy:

 from Import Register_converter, Path  from Import  'yyyy'= [    path ('articles/2003/  ', views.special_case_2003),    path ('articles/<yyyy:year>/  ', views.year_archive),    ...]
Using regular expressions

Django2.0 's URL has been changed to "configured", but it is still compatible with older versions. And this compatible approach is to use re_path() methods instead of path() methods. re_path()method in the bones, is simply the previous url() method, but the location of the import has changed. Here is an example, compare the grammar of the age of Django1.11, what is the difference?

 fromDjango.urlsImportPath, Re_path from.ImportViewsurlpatterns=[Path ('articles/2003/', views.special_case_2003), Re_path (R'^articles/(? P<YEAR>[0-9]{4})/$', Views.year_archive), Re_path (R'^articles/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/$', Views.month_archive), Re_path (R'^articles/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/(? p<slug>[\w-]+)/$', Views.article_detail),]

path()different from the method is the two point:

    • The year matches less than 10000 non-four digits, which is determined by the regular expression
    • All parameters passed to the view are string types. Instead path() of converting to some type, as you can specify in a method. Be careful when you receive parameters in the view.
Summarize

In addition to the above section, Django2.0 routing system other knowledge points and Django1.11 Basic are the same, that is, in the wording of the above there are so little difference.

So far. Reprint please indicate the source.

[Reference: Http://www.liujiangblog.com]

Django 2.0URL New Configuration Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.