Site management
-
Site: divided into two parts: content publishing and public access
-
The content publishing section is responsible for viewing, adding, modifying, and deleting data by the website administrator
-
Django can automatically generate management modules based on defined model classes
-
To use Django's management module, you need to follow these steps:
1. Localization of management interface
2. Create an administrator
3. Register the model class
4. Publish content to the database
1. Localization of management interface
-
Localization is the habit of using the displayed language, time, etc. locally. The localization here is to be Chinese
-
Simplified Chinese is used in Mainland China, and the Asian / Shanghai time zone is used for the time zone.Note that Beijing time zone is not used here.
-
Before localization:
-
After localization:
2. Create an administrator
-
Command to create an administrator:
python manage.py createsuperuser
-
Follow the prompts to enter your username, email, and password
-
reset Password
python manager.py changepassword 用户名
-
Landing site: http://127.0.0.1:8000/admin
The server needs to be started
-
Login to the site successfully
There is no book and character management entrance in the site interface, because there is no registered model class
3. Register the model class
-
Register the model class in the application's admin.py file
Need to import model modules: from book.models import BookInfo, PeopleInfo
-
After registering the model
注册模型成功后, 就可以在站点管理界面方便快速的管理数据.
4. Publish content to the database
-
After publishing content, the optimized model class is displayed
-
# 准备书籍列表信息的模型类 class BookInfo(models.Model): # 创建字段,字段类型... name = models.CharField(max_length=10) def __str__(self): """将模型类以字符串的方式输出""" return self.name
Views and URL
-
The site management page is ready, and the next step is to make a publicly accessible page.
-
MVT for Django's design framework.
- The user requests the view in the URL.
- The view processes the request after receiving it.
- And return the processing result to the requester.
-
Two steps are required to use the view
1. Define the view
2. Configure URLconf
1. Define the view
-
A view is a Python function that is defined in the views.py of the application.
-
The first parameter of the view is the HttpRequest type object reqeust, which contains all the request information.
-
The view must return an HttpResponse object, containing the response information returned to the requester.
-
Need to import HttpResponse module: from django.http import HttpResponse
-
Define the view function: response string OK! To the client
思考 : 如何才能让请求找到视图?
2. Configure URLconf
- The process of finding a view:
-
The requester enters the URL in the browser address bar and requests to the website.
-
The website obtains URL information.
-
Then match the written URLconf one by one.
-
If the match is successful, the corresponding view is called.
-
If all URLconfs are not matched successfully, a 404 error is returned.
-
URLconf
Entrance
-
Two steps are required to complete the URLconf configuration
- Define URLconf in the project
- Define URLconf in the application
-
Define URLconf in the project
-
Define URLconf in the application
提示:一条URLconf包括URL规则、视图两部分
-
URL rules are defined using regular expressions.
-
The view is the view function defined in views.py.
-
URL matching process
-
3. Test: Request access
http://127.0.0.1:8000/
4. Summary
The view processing process is as follows:
使用视图时需要进行两步操作,两步操作不分先后
配置URLconf
在应用/views.py中定义视图