Four Methods for Django to pass parameters through URLs

Source: Internet
Author: User

[From ian's personal blog] http://www.icodelogic.com /? P = 501

Thank you for your summary!

1. No Parameters
Configure the URL and its view as follows:
(R' ^ hello/$ ', hello)
Def hello (request ):
Return HttpResponse ("Hello World ")
Access http: // 127.0.0.1: 8000/hello and the output is "Hello World"

2. Pass a parameter
Configure the URL and its view as follows:
(R' ^ plist/(. +)/$ ', helloParam)
Def helloParam (request, param1 ):
Return HttpResponse ("The param is:" + param1)
Access http: // 127.0.0.1: 8000/plist/china. The output result is "The param is: china"

3. Pass multiple parameters
In the second case, take passing two parameters as an example. Configure the URL and its view as follows. In the URL, specify two parameters through regular expressions:
(R' ^ plist/p1 (\ w +) p2 (. +)/$ ', helloParams)
Def helloParams (request, param1, param2 ):
Return HttpResponse ("p1 =" + param1 + "; p2 =" + param2)
Access http: // 127.0.0.1: 8000/plist/p1chinap22012/
The output is "p1 = china; p2 = 2012 ″

We can see from this that the view parameters are matched in order based on the regular expression of the URL and are automatically assigned values. Although multiple parameters can be passed in this way, the URL is not flexible enough and looks messy. In addition, due to regular matching, errors may occur in some cases.

4 through the traditional "?" PASS Parameters
For example, http: // 127.0.0.1: 8000/plist /? P1 = china & p2 = 2012 '? 'Is the passed parameter. Here, the p1 and p2 parameters are passed.
Passing parameters in this way will not cause problems caused by regular expression Matching errors. In Django, the parsing of such parameters is obtained through the request. GET. get method.
Configure the URL and its view as follows:
(R' ^ plist/$ ', helloParams1)
Def helloParams (request ):
P1 = request. GET. get ('p1 ')
P2 = request. GET. get ('p2 ')
Return HttpResponse ("p1 =" + p1 + "; p2 =" + p2)
The output result is "p1 = china; p2 = 2012 ″

Related Article

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.