view
- Background management page done, next to do the public access to the page
- With Djangos design framework MVT, the user requests a view in the URL, the view receives the request, processes it, and returns the result to the requester
- There are two steps to using a view
- Defining a view
- Configuration URLconf
1. Define the view
- A view is a Python function defined in views.py
- The first argument to the view is an object of type HttpRequest, reqeUST, which contains all requested information
- The view must return an HttpResponse object containing the response information returned to the requester
- Open booktest/views.py and define view index as follows
#coding:utf-8
from django.http import HttpResponse
def index(request):
return HttpResponse("index")
Copy the code
2. Configuration URLconf
- The process of view search: the requestor enters the URL in the address bar of the browser. After the request is received, the requestor obtains the URL information and matches it with the prepared URLconf one by one. If the match is successful, the corresponding view is called
- An URLconf consists of URL rules and views
- Url rules are defined using regular expressions
- A view is a view defined in views.py
- A two-step URLconf configuration is required
- Define URLconf in the application
- Included in the project’s URLconf
- 1. Create urls.py under booktest/ app and define the code as follows
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
]
Copy the code
- 2. Include it in your project: Open the test1/urls.py file and add items to the list of urlPatterns as follows
url(r'^', include('booktest.urls')),
Copy the code
- The full code of the test1/urls.py file is shown below
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('booktest.urls')),
]
Copy the code
Request access
- With the view and URLconf defined, enter the url in the browser address bar
http://127.0.0.1:8000/
Copy the code