1. View
Admin page well, then do a publicly accessible page. When we just entered http://127.0.0.1:8000/admin/ in the browser, the browser displays the login page background management, the server is how to find this page and return it. / Admin / is the page we want to request the server after receiving the request, it must correspond to a processing operation, the processing operation is to help us produce content of the page and return back, this process is done by the view of.
Django design framework for MVT, the URL the user requested is a view, a rear view of receiving a request for processing, and returns the processing result to the requester.
1.1 Definitions view
Python is a view of a function that takes a Web request and returns a Web response. The response may be a page's HTML content, a redirect, a 404 error, an XML document, or a picture... Anything is possible. No matter what the view itself contains logic, it must return a response. Write the code where it does not matter, as long as it is in your Python directory. In order to put the code somewhere, will view the agreement is placed in the project or application files in the directory named in views.py.
View must have a parameter, usually called the request, the view must return HttpResponse object parameter HttpResponse content in the browser will be displayed on the page.
Open booktest / views.py file, define a view index:
from django.http import HttpResponse def index(request): return HttpResponse("index")
1.2 Configuration URLconf
Requester url in the browser address bar input, a request to the Web site for information url, then one by one good match and URLconf written, if the match succeeds view corresponding function is called, if not all URLconf match succeeds, it returns 404 error.
Url include a URLconf rules, views of two parts:
- url defined rules using regular expressions.
- View view function is defined in the views.py.
URLconf configuration requires two steps:
- 1. Definitions In the application URLconf
- 2. The project included URLconf
Urls.py file created in booktest / Application:
from django.conf.urls import url from booktest import views urlpatterns = [ url(r'^$', views.index), ]
djangotest / urls.py document modify the code as follows:
from django.contrib import admin from django.urls import path from django.conf.urls import include, url urlpatterns = [ path('admin/', admin.site.urls), url(r'^', include('booktest.urls')), ]
1.3 requesting access
View and URLconf defines Well, then start the server.
python manage.py runserver
Then enter the URL in the browser address bar:
http://127.0.0.1:8000/
Page is shown in the following figure, the view has been successfully executed.
2. Templates
In Django, the front-end content definition in the template, and then call the template to view a variety of beautiful, cool effects appeared.
Why use a template it?
-
Any changes to the page design must make the appropriate changes to the Python code. Site design modifications are often much more frequently than change the underlying Python code, so if the design can be changed without modify the Python code, it will be much more convenient.
-
Python code written in HTML and design are two different jobs, and most professional Web development environments will assign them to different people (and even different departments) to complete. Designers and HTML / CSS coders should not be asked to edit Python code to get their job done.
-
Writing Python code and designers making templates two programmers work simultaneously efficiency is the highest, far better than to let a person waiting for the other to finish editing work on a file that contains both Python and HTML are included.
For these reasons, the design of the page and code separation Python's much cleaner and easier to maintain.
2.1 Create a template
Create a template for the view index index.html under the application booktest.
Find the path set template: Open djangotest / settings.py file, set the value TEMPLATES of DIRS.
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
2.2 定义模板
templtes/booktest/index.html文件:
<html> <head> <title>图书列表</title> </head> <body> <h1>{{title}}</h1> {%for i in list%} {{i}}<br> {%endfor%} </body> </html>
在模板中输出变量语法如下,变量可能是从视图中传递过来的,也可能是在模板中定义的。
{{变量名}}
在模板中编写代码段语法如下:
{%代码段%}
2.3 视图调用模板
打开booktest/views.py文件,调用上面定义的模板文件:
from django.shortcuts import render def index(request): context = {'title': '图书列表', 'list': range(10)} return render(request, 'booktest/index.html', context)
打开浏览器刷新页面。