This article is participating in Python Theme Month. See the link for details

Eraser, one of the funny advanced Internet nerds. New series, let’s get into the world of Django.

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 snowball Learning Python, so you’ve already covered the basics of Python in Snowball Learning. Here are some of the things you might need to quickly recall, or you might want to open up the Snowball learning section.

  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. The corresponding document reference: docs.djangoproject.com/en/3.1/

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
print(django.get_version())
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 manage.py file, the management script for the project.

  • my_website
    • my_website
      • __init__.py
      • asgi.py: ASGI server deployment information
      • setting.py: Indicates the project configuration information
      • urls.py: Indicates the site routing Settings
      • wagi.py: Deployment information
    • manage.py

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

python manage.py runserver
Copy the code

Open your browser to http://127.0.0.1:8000/ and get something like the following, indicating that Django is already on.

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

python manage.py runserver 8080
Copy the code

Use Ctrl+C to stop the service.

Blogger ID: Dream eraser, hope you like, comment, favorites.