Django Book Learning notes-View and URL configuration

Source: Internet
Author: User

1. View

A view is a function of Python, and each view function must have at least one parameter, usually called a request. This is an object that triggers this view, contains the current Web request information, and is an instance of the class django.http.HttpRequest . It returns a HttpResponse instance. In order for a Python function to be a Django-aware view, it must satisfy both of these conditions. (There are exceptions)

Cases:

From django.http import httpresponsedef Hello (Request): Return HttpResponse ("Hello World")

2.URL Configuration

1). URLconf is like a directory of Django-supported websites. Its essence is the URL pattern and the mapping table between the view functions to invoke for that URL pattern. That's how you tell Django to call that code for that URL and call that code for that URL.

From django.conf.urls import patterns, include, urlfrom mysite.views import  hello# uncomment the next two lines to enable the admin:#  from django.contrib import admin# admin.autodiscover () urlpatterns =  Patterns ("',     # 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)) ,     (R ' ^hello/$ ',Hello),) 

2). URL configuration and Loose coupling

In a Django application, the definition of the URL and the view function are loosely coupled, in other words, deciding which view function the URL returns and implementing this view function is in two different places. This allows the developer to modify a piece without affecting another piece.

3). Dynamic URL

We use parentheses to identify the parameters in the URL pattern. In this example, we want to use these numbers as arguments, enclosing \d{1,2} in parentheses:

(R ' ^time/plus/(\d{1,2})/$ ', Hours_ahead),

Corresponding View functions:

From django.http import Http404, Httpresponseimport datetimedef hours_ahead (Request, offset): try:offset = Int (    Offset) except valueerror:raise Http404 () dt = Datetime.datetime.now () + Datetime.timedelta (hours=offset) html = "

Explanation:offset is extracted from the matching URL. For example: If the request URL is/time/plus/3/, then offset will be 3, if the request URL is/time/plus/21/, then offset will be 21. Note that the captured value is always a string (string) type, not an integer type, even if the string is entirely composed of numbers (for example: "21").

4). Regular expressions commonly used in URLs

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/B1/wKiom1W7Y-eB7VqyAAJPCFf4JGc607.jpg "title=" Cf{g~s ' Kv[nypc8qcm5r.png ' width= "817" height= "373" border= "0" hspace= "0" vspace= "0" style= "WIDTH:817PX;HEIGHT:373PX;" alt= "Wkiom1w7y-eb7vqyaajpcff4jgc607.jpg"/>

3.django Processing Request Process

1). Incoming requests are transferred to/hello/.

2). Django determines the root urlconf by root_urlconf configuration.

3). Django finds the first entry that matches the/hello/in all the URL patterns in urlconf.

4). If a match is found, the corresponding view function is called

5). The view function returns a HttpResponse

6.) Django conversion HttpResponse to a suitable HTTP response, displayed as a Web page

Django Book Learning notes-View and URL configuration

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.