In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how Python Django obtains the data in URL. I hope you will get something after reading this article. Let's discuss it together.
Django acquires data in URL
Parameters in URL generally come in two forms. As follows:
1. Https://zy010101.blog.csdn.net/article/details/1208169542. Https://so.csdn.net/so/search?q=Django&t=blog&u=zy010101
We call the first form "URL path parameters"; the second form is called "URL keyword form". Here's how to get these two forms of data in Django.
The path parameter of URL uses the path function from django.urls import pathfrom. Import viewsurlpatterns = [path ('articles/2003/', views.special_case_2003), path (' articles//', views.year_archive), path ('articles///', views.month_archive), path (' articles////', views.article_detail),]
For an explanation of this URL, please refer directly to the official Django documentation.
In case I can't get into the document sometimes, I post the official document directly below:
Use the re_path function
If using the path function does not meet your requirements for matching URL, you can use the re_path function to use regular expressions to match parameters in the URL path. Note that in Django, the syntax for using regular expressions to get values in a group is (? Ppattern), where name is the group name and pattern is the pattern to match.
From django.urls import path, re_pathfrom. Import viewsurlpatterns = [path ('articles/2003/', views.special_case_2003), re_path (r' ^ articles/ (? P [0-9] {4}) / $', views.year_archive), re_path (r'^ articles/ (? P [0-9] {4}) / (? P [0-9] {2}) / $', views.month_archive) Re_path (r'^ articles/ (? P [0-9] {4}) / (? P [0-9] {2}) / (? P [\ w -] +) / $', views.article_detail),]
For this description of URL configuration, please refer to the use of regular expressions
Similarly, in case I can't get into the document sometimes, I post the official document directly below:
It is important to note that after the regular expression is matched, the captured parameters are passed as strings to the view function (view class).
URL keyword form
In general, in addition to passing data in the URL path, you can also pass data in the URL parameter. For example:
Http://www.demo.com/index?keys=123&values=qwe
This URL passes the parameters keys and values, and their values are 123 QWE.
Before that, let's introduce the prerequisite QueryDict.
The properties GET and POST of HttpRequest object are objects of type QueryDict.
The Django get URL keyword parameter can be obtained through the HttpRequest.GET property. For example:
Def test (request): a = request.GET.get ("a") b = request.GET.get ("b") c = request.GET.get ("c") a_all = request.GET.getlist ("a") res = aura'
'+ baked'
'+ clockwise'
'+ str (a_all) return HttpResponse (res)
Now use the following URL to make the request:
Http://127.0.0.1:8008/test?a=1&a=2&b=3&c=4
The page appears as follows:
The query string does not distinguish the request mode, that is, if the client makes a request in POST mode, the query string data in the request can still be obtained through request.GET.
After reading this article, I believe you have a certain understanding of "how Python Django obtains the data in URL". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.