Home >Backend Development >Python Tutorial >How to use Django to reverse URLS parsing?

How to use Django to reverse URLS parsing?

不言
不言Original
2018-09-15 14:02:462427browse

The content of this article is about how to use Django to perform URLS reverse parsing? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

URL and URI
URL: Uniform Resource Locator Equivalent to absolute path
URI: Uniform Resource Identifier Equivalent to relative path
- URL under multiple Apps Configuration method
Configuration under the project directory

 url(r'^App/',views.hello)

You need to import the package here, use the alt enter key to import the package
Under the App project

 url(r'^getinfo/',views.getinfo)

Also here If you need to import the package, use the alt enter key to import the package
If you do not add '/' to the url when doing regular matching, there will be a matching error and you will get a fake page
-get request with parameters URl Passing parameters
url('?P9a638e57bd3075e0a0acbe78463659d6regular matching'/,views.function name) Binding parameters
For example, search for beautiful women through Baidu http://www.baidu.com/?wd=" Beauty"
needs to be followed by "?Request parameter"
Passing with parameters
Keyword parameter writing method

(?P<year>\d+)
  url(r&#39;^hi/(?P<year>\d+)/(?P<month>\d+)/(?                    P<day>\d+)/&#39;,views.gethi)

- URL reverse analysis:
1, What is url reverse parsing
A way to dynamically obtain URLs. For example: dynamically obtain the address of other movie websites. If the other party changes the URL, we also need to modify it, but if reverse parsing is used, the dynamic acquisition situation No matter how the other party changes, we can get it
2. Reverse parsing configuration:
Configure in the App directory urls and add the name attribute name ='xxx'
In the urls in the project directory, Configure another namespace="name customization". When configuring, please note that namespace is in include
url(r'^Two/', include('Two.urls',namespace="fan"))
3. Jump method:
- If you need to jump with parameters, if the parameter is a positional parameter, use the args parameter
- If the parameter is a keyword parameter, use kwargs to transfer the parameter in the form of a dictionary

  超链接跳转,: <a href =  &#39; {% url &#39; namespace: name &#39; %}&#39;></a>

Jump in the view function views:
1. Use HttpResponseRedirect('path address')--->fixed acquisition (the address value is fixed)
2. Use HttpResponseRedirect( reverse('namespace: name')) method dynamic acquisition (reverse analysis)

The above is the detailed content of How to use Django to reverse URLS parsing?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn