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:
- Management background, can be simple configuration, can realize a content management platform;
- ORM, object relational mapping, convenient database operation;
- 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.
- The operator;
- Data types, integers, strings, lists, tuples, dictionaries, collections;
- Key types, strings, lists, and their object methods;
- Tuple and dictionary correlation method;
- Branch and loop statements;
- Modules and functions;
- Classes, properties, methods;
- Regular expression;
4. Do you still remember these foundations?
- There is a function in a mathematical operator
divmod
Do you know how to use it? - Do dictionaries have order,
collections.OrderedDict
What do you do? - How are dictionary defaults set?
- deque
collections.deque
Have you studied? - use
os
Can modules import modules? - Does Python find variables in LEGB order?
- In the class
__
What does the underline mean?_
What does the underline mean? - Deep and shallow replication;
exec
.eval
.repr
What do the three built-in functions do?pickle
,json
,shutil
How 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
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 informationsetting.py
: Indicates the project configuration informationurls.py
: Indicates the site routing Settingswagi.py
: Deployment informationmanage.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 and visit:http://127.0.0.1:8000/, resulting in the following figure, which represents DjangoEnabled.
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.