Getting started with Django (2) the first view in Django

Source: Internet
Author: User

1. Create a view

Go to the second mysite directory. Create a new view. py file.

The Code is as follows:

From Django. Http import httpresponse

Def Hello (request ):

Return httpresponse ('Hello World ')

 

You can see that the View File introduces the module HTTP from Django and imports the httpresponse function. A Hello function in the view. The reutrn httpresponse can be interpreted as response. Write in C.

 

2. Create a route

Match the view from the route using the address of the browser.

Configure the route in the URLs. py file.

From Django. conf. URLs import patterns, include, URL

From mysite. Views import *

Urlpatterns = patterns ('',

(R' ^ Hello/', hello ),

# Examples:

# URL (R' ^ $ ', 'mysite. Views. home', name = 'home '),

# URL (R' ^ mysite/', include ('mysite. Foo. urls ')),

 

# Uncomment the admin/DOC line below to enable admin documentation:

# URL (R' ^ admin/doc/', include ('django. contrib. admindocs. urls ')),

 

# Uncomment the next line to enable the admin:

# URL (R' ^ admin/', include (Admin. Site. URLs )),

)

The red lines on the second line above are the routes configured for the hellp view. (R' ^ Hello/', hello), R indicates ignoring escape characters, which is similar to @ in C. The red part indicates the address of the browser, which is supported by regular expressions. Use a regular expression to match the browser address. Then, if the matching address is successful, route the request from this address to the view in the blue section. That is, if the browser accesses hello, it will match the hello view. Note that the hello function in views. py is used, so the views. py module must be loaded first. That is, the first line in the Code is red.

 

3. access in the browser

Enter http: // 127.0.0.1: 8000/hello in the browser

The word "Hello world" is displayed.

4. Return the server time page

Same as above. Try to access time from a browser. There must be a Time Matching regular, namely (R' ^ time/$ ', current_datetime), in red. After Entering time in the browser, you should be able to find the matched route. If this route is found, a view should be called. You can name the view name as needed. The view name here is current_datetime. Now that the route is available, define the current_datetime view.

Open views. py. Create a function

Def current_datetime (request ):

Now = datetime. datetime. Now ()

Html = "<thml> <body> it is now % S. </body>

Return httpresponse (HTML)

Before using it, you must first import datetime.

Import datetime

Then, the access address in the browser is http: // 127.0.0.1: 8000/time/

 

 

 

 

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.