How the Django note URL is distributed

Source: Internet
Author: User

Made 2 pages, think about the parameters and how the page passed, we look at the principle.

An HTTP request is sent to http://127.0.0.1:8000/hello/

Django will be the first to find the setting file setting.py


root_urlconf = ' untitled2.urls '



650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7A/C5/wKiom1a0ccqw9Oz3AAImpcsOqdY312.png "/>

When accessing url/hello/, Django loads the URLCONF according to the root_urlconf settings. And then sequentially match urlconf.

Urlpatterns until a match is found. When the matching urlpatterns is found, the associated view function is called and the

The HttpRequest object as the first parameter. (Give more information about HttpRequest later) (we'll see later

Standard of HttpRequest)

As we saw in the first view example, a view function must return a HttpResponse. Once done, Django will finish

into the remaining converted Python object to a suitable web Response with HTTP headers and body, (for example, Web content).


To summarize:

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 URL patterns in urlconf.

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

5. The view function returns a HttpResponse

6. The Django conversion HttpResponse is a suitable HTTP response, displayed as a Web page





When a dynamic view is generated, for example

Import datetime

now = Datetime.datetime.now ()

Now

Datetime.datetime (2016, 12, 13, 14, 9, 39, 2731)

>>> Print Now

201626 14:09:39.002731

In fact, these are Python code, and Django doesn't explain any code.

view.py

From django.http import httpresponseimport datetimedef Hello (Request): Return HttpResponse ("Hello World") def Current_ DateTime (Request): now = Datetime.datetime.now () HTML = "


urls.py

From django.conf.urls.defaults import *from mysite.views import Hello, current_datetimeurlpatterns = Patterns (', ' ^ hello/$ ', hello), (' ^time/$ ', current_datetime),)


of the build date http Request through URL Steering View , return to foreground after generating results


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7A/C5/wKioL1a0ch3zKPDbAACoshESNBs203.png "/>

This article is from the "Hao (pcdog) blog" blog, make sure to keep this source http://433266.blog.51cto.com/423266/1741260

How the Django note URL is distributed

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.