Python Advanced (36)-web Framework Django Project construction whole process
?? IDE Description:
- Win7 system
- python:3.5
- django:1.10
- pymysql:0.7.10
- mysql:5.5
?? Django is a free open source website framework developed by Python that can be used to quickly build high-performance, elegant websites!
Django Features
- Powerful Database capabilities
- With Python's class inheritance, a few lines of code can have a rich, dynamic database manipulation interface (API), and you can execute SQL statements if needed.
- Comes with powerful back-Office features
- A few simple lines of code let your site have a powerful background, easy to manage your content! An elegant website
- Use regular match URLs, pass them to the corresponding functions, and define them as you like!
- Template System – A powerful, easy-to-extend template system designed for ease of design, code, style, and easier to manage.
- Cache system – Used with memcached or other cache systems for better performance and faster load times.
- Internationalization – fully supports multi-lingual applications, allowing you to define translated characters and easily translate them into different countries ' languages.
?? The installation of the Python,mysql is no longer elaborated here, you can find the solution on the network itself. Where the Django,pymysql installation is done using the PIP Install * command.
?? Once Django is installed, you can use the DJANGO-ADMIN.PY management tool to create a project. First, let's take a look at django-admin.py's command introduction, and at the command line enter django-admin.py to see the available project management commands.
?? The specific process for creating a HelloWorld project for a Django project is as follows:
?? STEP1: Before building a Django project, first select the project storage directory. Then switch to the project storage directory in the DOS window CD.
?? STEP2: Create project execution django-admin.py Startproject HelloWorld
?? Open idea and see the project directory you created as shown:
?? Catalogue Description:
- HelloWorld: The container for the project.
- manage.py: A useful command-line tool that allows you to interact with the Django project in a variety of ways.
- helloworld/init. PY: An empty file that tells Python that the directory is a python package.
- helloworld/settings.py: Setup/configuration for this Django project.
- helloworld/urls.py: URL declaration for the Django project; A "directory" of Django-driven Web sites.
- helloworld/wsgi.py: A WSGI-compatible WEB server's portal to run your project.
?? Next we go into the HelloWorld directory and enter the following command to start the server:
pythonmanage.pyrunserver 0.0.0.0:8000
?? 0.0.0.0 allow other computers to connect to the development server, 8000 is the port number. If not stated, the port number defaults to 8000.
?? In the browser input your server's IP and port number, if normal startup, the output is as follows:
?? STEP3: Create app at command line input django-admin.py Startapp demo
?? Open idea and see the project directory you created as shown:
?? Catalogue Description:
- Demo: The application's container. Note: The following page design file, created in this directory templates, the file named Xx.html is placed here.
- init. PY: As on the previous init. py file
- Migrations: Database related directories, data classes will appear after synchronizing the database.
- Admin.py:admin Background Management files
- Apps.py:app Application Management Files
- models.py: The main use of a Python class to describe a data table, called model. Using this class, you can create, retrieve, update, and delete records in a database with simple Python code without having to write one or two SQL statements.
- tests.py: Test File
- views.py: Contains the business logic for the page.
Server-side response client request process
?? The flowchart is as follows:
?? The flowchart above can roughly describe the process of Django processing request, according to Flowchart 2, can be divided into the following steps:
- The user requests a page through the browser.
- 2. Requests arrive at request Middlewares, the middleware does some preprocessing to the request or response requests directly.
- 3.URLConf find the corresponding view via the urls.py file and the requested URL.
- 4.View middlewares is accessed, it can also do some processing of request or directly return to response.
- 5. Call the function in view.
- The methods in 6.View can selectively access the underlying data through models.
- 7. All model-to-db interactions are done through the manager.
- 8. If necessary, views can use a special context.
- 9.Context is passed to the template to generate the page.
- A.template using filters and tags to render output
- B. The output is returned to the view
- C.httpresponse is sent to response middlewares
- D. Any response middlewares can enrich response or return a completely different response
- E.response returned to the browser, presented to the user
URL () function
?? The Django URL () can receive four parameters, which are two required parameters: Regex, view, and two optional parameters: Kwargs, name, followed by the four parameters in detail.
- Regex: A regular expression that matches a URL that executes the corresponding second parameter view.
- View: Used to perform a URL request that matches a regular expression.
- Kwargs: The parameter of the dictionary type used by the view.
- Name: Used to reverse get the URL.
Django Project Deployment
?? In the previous introduction we used the Python manage.py runserver to run the server. This is used only in test environments.
?? The official release of the service, we need a stable and continuous server, such as Apache, Nginx, LIGHTTPD, etc., this article will take Nginx as an example.
Learning materials
?? http://www.ziqiangxuetang.com/django/django-tutorial.html
Python Advanced (36)-web Framework Django Project construction whole process