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