Django site management-Admin, view and URL

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:

    Insert picture description here

  • After localization:

    Insert picture description here

2. Create an administrator
  • Command to create an administrator:

    python manage.py createsuperuser
    
  • Follow the prompts to enter your username, email, and password

    Insert picture description here

  • reset Password

    python manager.py changepassword 用户名
    
  • Landing site: http://127.0.0.1:8000/admin

    The server needs to be started

    Insert picture description here

  • Login to the site successfully

    There is no book and character management entrance in the site interface, because there is no registered model class

    Insert picture description here

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

    Insert picture description here

  • After registering the model

    Insert picture description here

注册模型成功后, 就可以在站点管理界面方便快速的管理数据.

4. Publish content to the database

Insert picture description here

  • 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
    

Insert picture description here




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.

  1. The user requests the view in the URL.
  2. The view processes the request after receiving it.
  3. 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 Insert picture description here

思考 : 如何才能让请求找到视图?

2. Configure URLconf

  • The process of finding a view:
  1. The requester enters the URL in the browser address bar and requests to the website.

  2. The website obtains URL information.

  3. Then match the written URLconf one by one.

  4. If the match is successful, the corresponding view is called.

  5. If all URLconfs are not matched successfully, a 404 error is returned.

Insert picture description here

  • URLconfEntrance
    Insert picture description here

  • Two steps are required to complete the URLconf configuration

  1. Define URLconf in the project
  2. Define URLconf in the application
  • Define URLconf in the project

    Insert picture description here

  • Define URLconf in the application

    提示:一条URLconf包括URL规则、视图两部分

    • URL rules are defined using regular expressions.

    • The view is the view function defined in views.py.

      Insert picture description here

    • URL matching process
      Insert picture description here

3. Test: Request access

http://127.0.0.1:8000/

Insert picture description here

4. Summary

The view processing process is as follows:
Insert picture description here

使用视图时需要进行两步操作,两步操作不分先后

	配置URLconf
	
	在应用/views.py中定义视图

Summarize the View and URL matching process

Insert picture description here

Published 125 original articles · Like 260 · Visits 120,000+

Guess you like

Origin blog.csdn.net/weixin_44685869/article/details/105353646