[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 ″