Python Web framework Django project to build the whole process
IDE Description:
- Win7 system
- Python:3.5
- Django:1.10
- Pymysql:0.7.10
- Mysql:5.5
Note: The installed library version information can be viewed via pip freeze.
Django is a free, open source web framework developed by Python that can be used to quickly build high-performance, elegant websites!
Django Features
- Powerful database functions
- With python class inheritance, in a few lines of code you can have a rich, dynamic interface (API) for database manipulation, and you can also execute SQL statements if needed.
- Self-contained powerful back-office functions
- A few simple lines of code gives your website a powerful backend to easily manage your content! Elegant URLs
- Match the URL with a regular, pass it to the corresponding function, and define it at will, as you wish!
- Template system - powerful, easy to expand the template system, easy to design, code, style separate design, easier to manage.
- Caching system - Co-link with memcached or other caching systems for better performance and faster loading.
- Internationalization - Fully supports multi-language applications, allowing you to define the characters to be translated and easily translate into different national languages.
About Python, MySQL installation operation here will not be elaborated, you can find their own solution on the network. Among them, the installation of django, pymysql can be completed using pip install * command.
Once Django is installed, you can use the administration tools to create a project. First, let's take a look at the commands available at the command line to see what's available.
The process of creating a HelloWorld project in Django is as follows.
Step1: Before building a Django project, first select the project storage directory. Then switch to the project storage directory in Dos window CD.
Step2: Create the project Execute startproject HelloWorld
Open IDEA and you can see the project directory created as shown below:
Catalog Description:
- HelloWorld: The project's container.
- : A useful command line tool that lets you interact with this Django project in a variety of ways.
- HelloWorld/: An empty file that tells Python that the directory is a Python package.
- HelloWorld/: Settings/configuration for this Django project.
- HelloWorld/: URL declaration for this Django project; a Django-driven website "catalog".
- HelloWorld/: An entry point to a WSGI-compliant web server to run your project.
Next we go to the HelloWorld directory and enter the following command to start the server:
python runserver 0.0.0.0:8000
0.0.0.0 allows other computers to connect to the development server. 8000 is the port number. If not specified, the port number defaults to 8000.
Enter the ip and port number of your server in the browser, and if it starts normally, the output will be as follows:
Step3: Create the application Type startapp demo at the command line
Open IDEA and you can see the project directory created as shown below:
Catalog Description:
- demo: Container for the application. Note: For the page design files in the back, create the directory templates in this directory and put the files named here.
- :: As in the previous document
- migrations: Database-related directories, where data classes appear after synchronizing the database.
- : admin backend management files
- :: app application management files
- The main Python class used to describe the data table is called model. With this class, you can create, retrieve, update, and delete database records with simple Python code without having to write SQL statement after SQL statement.
- :: Test files
- : Contains the business logic of the page.
Create Super Administrator
python createsuperuser # Follow the prompts to enter the user name and corresponding password on it mailbox can be left blank, user name and password required # Modify The user password is available: python changepassword username
be attached
The process of the server responding to client requests
The flowchart is shown below:
The above flowchart can roughly describe the process of Django processing request, according to the labeling of flowchart 2, can be divided into the following steps:
1. The user requests a page through the browser.
2. The request reaches Request Middlewares, middleware to do some preprocessing on the request or directly respond to the request.
Find the corresponding View by the file and the requested URL.
Middlewares is accessed, it can also do some processing of the request or return a response directly.
5. Call the function in View.
The methods in can optionally access the underlying data through Models.
7. All Model-to-DB interactions are done through the manager.
8. If desired, Views can use a special Context.
is passed to Template for page generation.
Using Filters and Tags to Render Output
b. The output is returned to the View
Being sent to Response Middlewares
d. Any Response Middlewares can enrich the response or return a completely different response.
Return to the browser and present it to the user
url() function
Django url() can receive four parameters, are two mandatory parameters: regex, view and two optional parameters: kwargs, name, the next detailed description of these four parameters.
- regex: Regular expression that executes the second parameter, view, on URLs that match it.
- view: Used to execute URL requests that match a regular expression.
- kwargs: Arguments of dictionary type used by the view.
- name: Used to reverse the URL.
Django Project Deployment
In the previous introduction we used python runserver to run the server. This is only suitable for use in a test environment.
The official release of the service, we need a stable and continuous server, such as apache, Nginx, lighttpd, etc., this article will be followed by Nginx as an example.
Setting up access to the project with your own iP address
1. First you need to execute > runserver 0.0.0.0:8000.
2. Inside you need to add ALLOWED_HOSTS="*".
This is the whole content of this article.