Django Basic Knowledge points

Source: Internet
Author: User

1. Knowledge of Django

  

1.Django is the direction of chatty, it is most famous for its fully automated management background: Just use the ORM, do simple object definition, it can automatically generate database structure, and full-featured management background.
The 2.Django built-in ORM has a high degree of coupling with other modules within the frame.
The application must use the Django built-in ORM, otherwise it will not be able to enjoy the various facilities provided within the framework based on its ORM;
Theoretically can be cut to replace its ORM module, but this is equivalent to the completion of the renovation of the house demolition, rather than the beginning of the hair embryo room to do a new decoration.
3.Django's selling point is the ultra-high development efficiency, its limited performance expansion, the use of Django Project, after the traffic reaches a certain scale, it needs to be reconstructed to meet the performance requirements.
4.Django is for small and medium-sized websites, or as a tool to quickly prototype a product as a large website.
The design philosophy of 5.Django templates is to completely separate code and style, and Django fundamentally eliminates the possibility of coding and processing data in templates.

Comparison of 2.Django, Flask and Tornado

1.Django Go is the direction of chatty, the development efficiency is high. Its MTV framework, with its own orm,admin back-office management, its own SQLite database and development testing server, has increased developer productivity by 2. Flask is a lightweight framework, free, flexible, and highly scalable, the core based on Werkzeug WSGI tools and jinja2 template engine 3.Tornado Walk is few but good direction, superior performance. It is best known for its asynchronous non-blocking design approach. Tornado's two core modules:    1.IOSTRAEM: A simple package of non-blocking sockets    2.ioloop: Encapsulation of I/O multiplexing, which implements a single case

3. What is WSGI,UWSGI,UWSGI?

1.wsgi,web Server Gateway Interface, is a set of protocols. Used to receive user requests and initial encapsulation of requests, and then submit the request to the Web framework (traffic rules that describe Web server and WebApplication)

  WSGI serverResponsible for receiving the request from the client, will be request forwarded application , return application the return response to the client;

2.uwsgi, like Wsgi, is a communication protocol that is an exclusive protocol to the UWSGI server that defines the type of transmission information

3.uWSGI, is a Web server that implements the WSGI protocol, the UWSGI protocol, the HTTP protocol,

Life cycle of 4.Django requests

 1.WSGI, the request is encapsulated and handed over to the web framework

2. Middleware, check the request object or add other data such as: CSRF

3. Route matching, matching different view functions according to different URLs

4. View functions, processing requests in view functions (may involve orm,template rendering)

5. Middleware, processing the data in response

6.WSGI, send the content of the response to the browser

5.FBV and CBV

 FBV: Function-based view

CBV: Class-based view

Advantages of using CBV:

1. Improved code reusability, can use object-oriented technology, such as (Multiple inheritance)

2. You can use different functions to process different HTTP methods instead of using If...else ... Judgment, improve readability

6. Add adorners to the CBV program

1. introduction of the Method_decorator module

2. Add adorners directly to the class

3. Add adorners directly to the method

7. Introduction to MVC and MTV

The MVC software system is divided into three parts: model, view, and controller

Model: Responsible for mapping of business objects and databases

View: Responsible for interacting with the user

Controller: Accept user's input call model and view complete user's request

MTV software system is divided into three parts: model, Template (Templates), view

Model: Responsible for mapping of business objects and databases

Template: Responsible for displaying the page to the user

View: Responsible for interacting with the user (call model and template when needed)

The role of name in the 8.Django routing system

The user parses the route in reverse, the equivalent of an alias to the URL, as long as the name does not change, even if the route changes, it can be found by this name route

9. Enumerate the built-in components of Django:

1.Admin is the component to be added to the corresponding data table in model

2.model components: Responsible for operating the database

3.form components: 1. Generate HTML code 2. Data validation 3 Check information back and show

The 4.ModelForm component is used for database operations and is also available for user-requested validation


Role and application scenarios of 10.Django middleware

Middleware is a process between request and response processing, which changes the Django input and output at the global scope.

In a nutshell, middleware helps us to do some extra work before and after the view function executes.

eg

    CSRF protection is enabled by default in a 1.Django project, and the CSRF middleware checks for the correct #token value in the request with each request

2. when the user sends the request on the page, through the custom authentication middleware, determines whether the user has logged in, has not logged on to log on.

Several methods of 11.Django middleware

1.process_request: When the request comes in, the permission authentication 2.process_view: After the route matches, can obtain the view function 3.process_exception: The exception is executed 4.process_template_ Responseprocess: Executing 5.process_response When a template is rendered: executing when a request responds

When was the request object of 12.django created (wsgi.py)

Class Wsgihandler (base. Basehandler):

    When request = Self.request_class (environ) requests to go to the Wsgihandler class, the __cell__ method is executed and the environ is encapsulated as the request

Implementation of 13.Django redirection

1. Using Httpresponseredirect

2. Using redirect and reverse

3. Status code: 301 and 302

301: Often used to do domain name jump (old resources have been removed, jump to new resources)

302: Often used to do temporary jumps (the resources to access are still there, but no conditional access)

Implementation mechanism of CSRF

First step: When Django responds to a request from a client for the first time, the backend randomly generates a token value that holds the token in the session state, and the backend puts the token in the cookie to the front page;

The second step: the next front-end need to initiate a request (such as Post) when the token value added to the request data or header information, together to the backend; Cookies:{csrftoken:xxxxx}

  Step three: Back-end check front-end request bring the token and the token in the session is consistent;

15 What method can I use to carry CSRF tokens when I send a POST request using AJAX based on Django?

In the form of a key-value pair in the data to be transferred

The 16.Django itself provides runserver, why can't it be used for deployment? (The difference between Runserver and UWSGI) 

The 1.runserver method is a common way to debug Django, which runs with Django's Wsgi Server, is used primarily in testing and development, and Runserver is a single-process open mode.
2.uWSGI is a Web server, it implements the WSGI protocol, UWSGI, HTTP and other protocols. Note that UWSGI is a communication protocol, and UWSGI is a WEB server that implements UWSGI protocols and WSGI protocols. UWSGI has the advantages of ultra-fast performance, low memory footprint and multi-app management, and with Nginx is a production environment, to be able to isolate user access requests and app apps, for real deployment. In contrast, the supported concurrency is higher, facilitating the management of multiple processes, maximizing the benefits of multi-core, and improving performance.

  

    

  

Django Basic Knowledge points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.