"Python Efficient Development Combat" practical walkthrough--Basic View 3

Source: Internet
Author: User

After the creation of the Django Project and the app, you can start writing the site app Code, which demonstrates the Django routing mapping feature by displaying a welcome title for the registration page.

1) First establish a route response function in djangosite/app/views.py:

From django.http import HttpResponse def welcome (Request):    returnhttpresponse ("

The code defines a function welcome () and simply returns a welcome information that is encapsulated by the HttpResponse ().

2) Next, bind the user's HTTP access to the function through the URL mapping.

Create a new urls.py file in the djangosite/app/directory to manage all URL mappings in the app app with the following file contents:

From Django.conf.urls import Urlfrom. Import views urlpatterns = [    url (r ', Views.welcome),]

In line 1th, the URL () function in Django.conf.urls was introduced, and all the route mappings in Django were generated by the function. The 2nd line of code introduces the djangosite/app/views.py module. The key variable, urlpatterns, is then defined, which is a list that holds all route mappings generated by the URL () function. Only one mapping is set in this code, and all routes are mapped to the welcome function in view.py.

3) Add an entry in the urlpatterns of the project URL file djangosite/urls.py, declaring a reference to the urls.py file in the app app, with the following code:

From django.conf.urls import urlfrom django.contrib import adminfrom django.conf.urls import include                                                       #本行新增 Urlpatterns = [    url (R ' ^app/', include (' App.urls ')),                                                 #本行新增   url (r ' ^admin/', admin.site.urls),]

First introduce the Django.conf.urls.include () function through the import statement, then add a path ' app/' to the Urlpatterns list and transfer it to the App.urls package, which is djangosite/app/ urls.py file. In this way, two urlpatterns are connected by using the Include () function.

Note: The 1th parameter of the URL () function uses a regular expression to express the URL route, in this case ' ^app/' means "all routes beginning with the app."

Interested readers can take a look at the structure of this book, "Python's Efficient development combat"

"Python Efficient Development Combat" practical walkthrough--Basic View 3

Related Article

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.