In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Django implements url reverse parsing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Where URL can appear in the code
One is in the HTML template and the other is in the view function:
1. In the template [HTML]
(1) when the hyperlink is clicked, the page jumps to URL
(2) the data in form form is submitted to URL by post method.
2. In the view function
(1) 302Jump HttpResponseRedirect (URL) redirects the address in the user's address bar to URL
2. URL writing specification in the code.
(1) absolute address
Eg: http://127.0.0.1:8000/page/1
(2) relative address
Relative address at the beginning of'/'eg:'/ page/3'
The browser will add this address to the protocol, Ip, port in the current address bar as the final access address.
For example, the current page address is: http://127.0.0.1:8000/page/1; the relative address is'/ page/3'; the final access address is: http://127.0.0.1:8000 + / page/3
Relative address eg: 'page/3' without the beginning of' /'
The browser will add the relative address as the final access address based on the last / preceding content of the current URL.
For example, the current page address is: http://127.0.0.1:8000/topic/detail; the relative address is' page/3'; the final access address is: http://127.0.0.1:8000/topic/ + / page/3
3. Reverse parsing of URL
(1) definition:
URL reverse parsing refers to using the name defined by path to dynamically find or calculate the corresponding route in the view or template.
Path function syntax:
Path (route, views, name=' alias')
Based on the keyword name in path, the unique name is determined to URL, from which the URL information can be inversely inferred in the template or view. This avoids writing absolute or relative paths in templates or views.
(2) Mode of use
Template-reverse address resolution through URL tags
{% url 'alias'%} {% url 'alias'% parameter value 1''parameter value 2'} eg: {% url 'pagen'' 100'%} {% url 'person' age='18' name='xixi'%}
View function-you can call the reverse method in Django for reverse parsing
From django.urls import reversereverse ('aliases', args= [], kwargs= {}) eg:reverse ('pagen',args= [100]) reverse (' person',kwargs= {'age':18,'name':'xixi'}) from django.urls import reverse url = reverse (' base_index') return HttpResponseRedirect (url) 4 cases
Urls.py
Path ('test/url', views.test_url), # reverse parsing the route path (' test_url_result/', views.test_url_result, name='tr') through 'tr' in the template test_url.html, and the route in the previous case # reverse parsing the route path (' base_index', views.base_view, name='base_index') through reverse in the View function test_url_result
Views.py
Def test_url (request): return render (request, 'test_url.html') def test_url_result (request, age): # 302Jump from django.urls import reverse # reverse parsing the route through reverse url = reverse (' base_index') return HttpResponseRedirect (url) # return HttpResponse ('--test url is ok') # this method can directly return to verify routes with / and missing / in relative addresses
Templates/test_url.html
Test url absolute address with'/ 'relative address without' / 'relative address
Url reverse parsing
To start the service, enter http://127.0.0.1:8000/test/url in the browser
After clicking the mouse, it is suspended on the corresponding connection, you can see the corresponding absolute address.
Thank you for reading! This is the end of this article on "how to achieve url reverse parsing in Django". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.