view

  • The view accepts the Web request HttpRequest, performs the logical processing, and returns the Web response HttpResponse to the requestor
  • A view is a Python function defined in the “apply /views.py” file
  • The response can be the HTML content of a web page, a redirect, a 404 error, etc

Creating a sample project

  • Create project test3
django-admin startproject test3
Copy the code

  • Go to the project directory and create the application BookTest
cd test3
python manage.py startapp booktest
Copy the code

  • Install the app under INSTALLED_APPS in test3/settings.py

  • In test3/settings.py, the DATABASES TAB is configured using MySQL database test2, which was created in Part 2

  • The TEMPLATES TAB in test3/settings.py configures the template lookup path

  • The template directory structure is as follows

URLconf

  • Visitors request a site by typing a url into the browser’s address bar, and for Django-developed sites, url matching determines which view handles the request

configuration

  • Specify url configuration via ROOT_URLCONF in test3/settings.py

  • Open test3/urls.py to see the default configuration

  • Include the configuration in test3/urls.py and create the specific configuration in each application

  • Defines a list of URlPatterns that store URL () objects, the name of which is fixed

  • Each regular expression in urlPatterns is compiled the first time they are accessed, making it fast to run

  • Create urls.py in the booktest directory

  • Create the home page URL in booktest/urls.py as follows

  • Create view index in booktest/views.py
#coding=utf-8
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse('hello world')
Copy the code

Get the value

  • The requested URL is treated as a plain Python string, and no domain name, GET, or POST parameters are included for matching
  • Each captured parameter is passed to the view as a plain Python string
  • If the requested address is as follows:
http://127.0.0.1:8000/1/?a=10
Copy the code
  • After removing the domain name and parameter parts, only the following parts are left to match the re
/ 1 /Copy the code
  • Open the booktest/urls.py file and define the URL that matches this address as follows

  • Create view show1 in booktest/views.py
def show1(requset):
    return HttpResponse('show1')
Copy the code

  • Start the server, enter the url above, the browser effect is as follows

  • To get a value, you need to use parentheses in the regular expression in two ways
    • Positional arguments
    • Keyword parameter
  • Note: Do not mix the two parameter methods. Only one parameter method can be used in a regular expression

Mode 1: Position parameters

  • Use the parentheses directly, passing them to the view as positional arguments
  • To extract the parameters, modify the regular expression above as follows
url(r'^(\d+)/$',views.show1)
Copy the code

  • Example Modify view show1 as follows
  • Note: parameter names are arbitrary (a1, b8)
def show1(requset, id):
    return HttpResponse('show %s' %id)
Copy the code

  • Refresh the browser page, as shown in the following figure

  • Enter the following address in the address bar
http://127.0.0.1:8000/2/?a=10
Copy the code

Method 2: Keyword parameters

  • Name the group in the regular expression section
  • Modify the regular expression as follows
  • In the? Part P indicates that the name defined for this parameter is ID, which can be any other name
url(r'^(? P<id1>\d+)/$',views.show1),Copy the code
  • Example Modify view show1 as follows
  • Note: The show1 view must have a parameter named id1, otherwise an error is reported
def show1(request,id1):
    return HttpResponse('show %s'%id1)
Copy the code
  • Refresh the browser page, as shown in the following figure