Article Directory
1. Introduction to Django framework
What is a framework?
- The software framework is to provide some basic software products for the realization or completion of some kind of software development.
- The function of the framework is similar to the infrastructure, providing and implementing the most basic software architecture and system
- Usually we implement more complex business program development based on the framework
- Two words, the framework is the programskeleton
What are the common frameworks in Python?
- Django is officially called the perfectionist's Web framework.
- Strive to streamline web.py and Tornado
- The new generation of micro-framework Flask and Bottle
Introduction to Django Framework
- Django is aAdvanced Python web framework, which encourages rapid development and clean, pragmatic design.
- Built by experienced developers, it takes care of many of the hassles of web development, so you can focus on writing your application without
reinventing the wheel.- Free and open source.
- Officially called a perfectionist web framework.
Django installation
Install Django online, specify version installation: pip install django==2.2
2. Background management
The first step: the creation and operation of the project
Step 2: Application creation and use
(1) Create the first application One
or more applications can be created in a project, and each application performs a business process.
The command to create the application:
python manage.py startapp bookApp
(2) Write the first view function
(3) Routing rules
To create a URLconf in the bookApp directory, create a file named urls.py
According to the configuration file BookManage/settings, it can be known that the main file for routing search is BookManage/urls.py, so add a url rule to this file
(4) Effect display
Start the server: python manage.py runserver
url() function introduction
Django url() can receive four parameters, two mandatory parameters: regex, view and two optional parameters:
kwargs, name.
- regex : regular expression, the matching URL will execute the corresponding second parameter view.
- view : Used to execute URL requests that match the regular expression.
- kwargs : The parameters of the dictionary type used by the view.
- name : Used to reverse fetch the URL.
Step 3: The database model of the project
Create a database model
This example completes the maintenance of "book-hero" information, and needs to store two types of data: book and hero
(1) Book table structure design: Table name: Book Book
name: title
Book release time: pub_date
(2) Hero table structure design: Table name: Hero Hero
name: name
Hero gender: gender
Hero profile: Book to which hcontent
belongs: hbook
Basic operation of database model
Step 4: Enable background Admin site management
- The site is divided into two parts: "content publishing (background management)" and "public access (front management)"
- The "content publishing" part is responsible for adding, modifying, and deleting content, and developing these repetitive functions is a tedious and uncreative
job.- As a result, Django fully automatically generates admin modules based on the defined model classes
There is a very powerful application function in the Django framework: automatic management interface. Often used by web platform managers to manage the entire
web platform.
By default, INSTALLED_APPS contains the following applications in the settings.py configuration file. For subsequent development,
these applications are included by default.
(1). Database migration
To use these Django-built applications, we need to create some data table correspondences in the database before we can use them.
3. Front desk management
Step 1: URLconf Routing Management
In Django, defining URLconf includes regular expressions and views.
Django uses regular expressions to match the requested URL, and if a match is found, calls the application's view.
Note: Only the path part is matched, that is, the string after the domain name and parameters are removed.
Add a subconfiguration file in the main configuration file, so that the main urlconf configuration is connected to the submodule's urlconf configuration file.
Step 2: View function handles business logic
In Django, views respond to WEB requests (response).
The view receives the reqeust request object as the first parameter, which contains the requested information.
A view function is a Python function defined in views.py.
After defining the view, you need to configure urlconf, otherwise the request cannot be processed.
Step 3: Template management to achieve a good-looking HTML page (refer to the rookie tutorial)
(1) Template engine configuration
Modify the BookManage/settings.py file and set the DIRS value of TEMPLATES (refer to the figure below)
(2) Template syntax: variable
(3) Template syntax: common tags
(4) Front-end HTML design of home page and detail page
Title tags (except enumeration, you can refer to the rookie tutorial to learn) list
tags
image tags
link tags
table tags form
tags
Fourth, the life cycle of Django requests
- wsgi: Encapsulate the request and hand it over to the back-end web framework (Flask, Django).
- request middleware: Verify the request or add other relevant data in the request object, such as: csrf, request.session.
- route matching: Match different view functions according to different urls sent by the browser.
- view function: The business logic is processed in the view function, which may involve: ORM, Templates.
- Response middleware: Process the response data.
- wsgi : Send the content of the response to the browser.