The page created on the helloworld custom page created in Django can only be regarded as a static page, initiate a request, return a fixed value, and cannot meet our dynamic needs. Today, we create a urlconf with parameters to display different contents based on the parameters.
Another attempted Function
Open views. py and create an attempt function.
1 DefCurrent_dt (request ):2Now =Datetime. datetime. Now ()3Html ="<HTML> <body> it is now % S. </body> "%Now4ReturnHttpresponse (HTML)
Here I use the views. py file of helloword, And the datetime module needs to be introduced.
FromDjango. HTTPImportHttpresponseImportDatetime
Open URLs. py and configure a URL
URL (R' ^ curtime/$ ', current_dt ),
Okay. Let's access this address. The effect is as follows:
The current time is displayed for each refresh. Of course this is not our dynamic effect.
View functions with Parameters
Adds a time difference view based on the current time.
We repeat the above action to create an attempt function (except for the request, there is one more parameter-time difference)
1 Def Hours_ahead (request, phours ): 2 Try : 3 Phours = INT (phours) 4 Except Vauleerror: 5 Raise Http404 () 6 7 Dt = datetime. datetime. Now () + datetime. timedelta (hours = Phours) 8 Html = " <HTML> <body> in % s hour (s), it will be % S. </body> " % (Phours, DT) 9 Return Httpresponse (HTML)
Note: The captured values (transmitted parameters) are always of the string type rather than the integer type, even if the string is composed of digits (such as "21 ").
So here we use int () for conversion
Phours = int (phours)
Configure urlconf
So, how do we design a program to deal with any number of time differences?
The answer is: Use the wildcard (wildcard urlpatterns ).A URL is a regular expression.Therefore, you can use d + to match more than one number.
Here we have set a limit of 99 hours.
Now we have designed a URL with a wildcard character. We need a method to pass it to the view function, so that we can process all the time periods with only one view function. We use parentheses to mark the parameters in URL mode. In this example, we want to use these numbers as parameters and use parentheses\ D {1, 2}Surrounded
URL (R'^ Ptime/plus/(\ D {1, 2})/$', Hours_ahead ),
Another focus, The start Letter of the regular expression string "R ". It tells Python that this is an original string and does not need to process the backslash (Escape Character) in it ). In a common Python string, the backslash is used to escape special characters. For example, escape N into a line break. After you use R to mark it as an original string, Python no longer regards the backslash as an escape character. That is to say, "N" is two strings: "" and "N ". Because the backslash conflicts between the Python code and the regular expression, we recommend that you use the original string when defining the Regular Expression in Python.
Okay. Let's take a look at the results.
So far, we can pass parameters to display the content according to the parameters.