The Django World of the Python Web

1. What is Django

Django is a framework for developing web sites using the Python programming language. Django provides common development templates that allow programmers to focus on the business side.

Djano advantages:

  1. Management background, can be simple configuration, can realize a content management platform;
  2. ORM, object relational mapping, convenient database operation;
  3. A template system with separate front and back ends;

More content, must be learned after in-depth understanding. Since Django is the main Python Web framework, it supports the MVC pattern. It’s easy to get caught up in the concept of MVC in its early stages. Here’s a brief explanation of the eraser.

MVC is an abbreviation of three words, model-view-Controller, which is a very mature Web application design pattern.

  • M: Responsible for obtaining data from the database;
  • V: Responsible for pushing data to users;
  • C: the controller that transfers data between M and V.

1.1 Django MVT mode

MVT is a design pattern that comes with Djongo. Unlike MVC, Django comes with HTML template files and supports the template language, which will be covered in this blog post. Django doesn’t have a lost Controller, it just implements it itself, without much developer involvement.

1.2 Other Python Web Frameworks

There are many Web frameworks based on Python, such as Webpy, Flask, Bottle, Pyramid and Tornado. The third round of snowball learning is mainly Djando, the framework is to improve the efficiency of code writing tools, from any learning can start, choose Django just because the framework is more popular in China, be sure to pay attention to, from the framework there is no good or bad points.

2. Pre-web development knowledge

Front-end knowledge front-end knowledge includes HTML, CSS, Javascript, it is recommended that students who want to learn the complete Web development, this part of the knowledge must be complete, very beneficial to you, if the opportunity later, eraser will separate a set of Web front-end columns.

HTTP protocol and TCP/IP protocol Web development is implemented in accordance with the HTTP protocol, so this part is also a necessary knowledge, the network protocol TCP/IP in the initial stage can be postloaded.

At present, the mainstream servers in China are Linux systems. Although there is no problem running Python on Windows, it is recommended to master the knowledge of Linux servers.

Database system as a Python developer, this part of the knowledge can not be omitted, especially MySQL database, in addition, skilled command of SQL language, is also a priority.

The cache system has a preference for Redis

Data structures and design patterns are the core competencies of programmers, and the beginner stage can be postdated, including algorithms.

Operating system principle is a course in the university, I believe many students have forgotten, here only to remind, if you want to own the way of programming, go further, suggest to supplement the operating system principle related knowledge.

3. Python for Web development

This column is an extension of Snowballing Python, so you’ve already covered Python in snowballing.

  1. The operator;
  2. Data types, integers, strings, lists, tuples, dictionaries, collections;
  3. Key types, strings, lists, and their object methods;
  4. Tuple and dictionary correlation method;
  5. Branch and loop statements;
  6. Modules and functions;
  7. Classes, properties, methods;
  8. Regular expression;

4. Do you still remember these foundations?

  1. There is a function in a mathematical operatordivmodDo you know how to use it?
  2. Do dictionaries have order,collections.OrderedDictWhat do you do?
  3. How are dictionary defaults set?
  4. dequecollections.dequeHave you studied?
  5. useosCan modules import modules?
  6. Does Python find variables in LEGB order?
  7. In the class__What does the underline mean?_What does the underline mean?
  8. Deep and shallow replication;
  9. exec.eval.reprWhat do the three built-in functions do?
  10. pickle,json,shutilHow to use modules?

5. Django setup and run

Installing Djando is extremely simple, using the command line to install it.

pip install django
Copy the code

If the installation is successful, the following information is displayed:

Successfully installed asgiref-3.3.1 django-3.1.7 sqlparse-0.4.1
Copy the code

Once installed, use the following code to test it:

import django
Copy the code

To test that the environment variables are correct, run the following command in a command window:

django-admin help
Copy the code

If everything looks like the above, go ahead and create a Django project using the following command.

django-admin startproject my_website
Copy the code

After the command is executed, the following file structure is generated in the corresponding folder.

The my_website root contains another my_website directory, the configuration file directory for the entire project, and the file, the management script for the project.

  • my_website
    • my_website
    • ASGI server deployment information
    • Indicates the project configuration information
    • Indicates the site routing Settings
    • Deployment information

From the command line, use CD to go to the my_website directory and run the following command:

python runserver
Copy the code

Open your browser and visit:, resulting in the following figure, which represents DjangoEnabled.

At run time, you can also specify ports, for example:

python runserver 8080
Copy the code

Use Ctrl+C to stop the service.