Python framework-Django entry to master tutorial

One, Django process

Key
MVT process: master the functions of each module of M, V, T, understand the MVT process,
Insert picture description herecreate Django projects and applications

django-admin startproject name

python manager.py startapp name

View and ULR

View request and response

URL matching path

1. Introduction to Django

1 Introduction

Django, pronounced [`dʒæŋɡəʊ], is an open source web development framework written in python and follows the MVC design. Lawrence Publishing Group developed this framework in order to develop a news content-based website, which was released under the BSD license in July 2005. The name comes from the Belgian jazz musician Django Reinhardt. He is a gypsy who mainly plays guitar and has also played violin.
Due to the rapid development of Django in recent years, it has become more and more widely used. It was selected as the 2013 SDTimes100 by SDTimes, a well-known IT development magazine, and ranked 6th in the category of "APIs, libraries and frameworks", and is considered to be the leader in this field.
Insert picture description here
The main purpose of Django is to develop database-driven websites easily and quickly. It emphasizes code reuse. Multiple components can easily serve the entire framework in the form of "plugins". Django has many powerful third-party plug-ins, and you can even develop your own toolkit easily. This makes Django highly extensible. It also emphasizes rapid development and DRY (DoNotRepeatYourself) principles.

2. Features

1) Heavyweight framework

Compared with the Flask framework, Django natively provides many functional components to make development easier and faster.

Provide automated scripting tools for project engineering management
Database ORM support (Object Relational Mapping, English: Object Relational Mapping)
template
Form
Admin management site
File management
Authentication authority
session mechanism
Cache

2) MVT mode

There is a programming pattern called MVC, whose core idea is division of labor and decoupling, reducing coupling between different code blocks, enhancing code scalability and portability, and achieving backward compatibility.

The full spelling of MVC is Model-View-Controller. It was first proposed by TrygveReenskaug in 1978. It is
a software design pattern invented by Xerox PARC for the programming language Smalltalk in the 1980s. It is designed by applying traditional input (input), processing (processing), and output (output) tasks to the graphical user interaction model. With the advent of standard input and output devices, developers only need to focus on the analysis and implementation of business logic. Later it was recommended as
the design pattern of Oracle's Sun company's Java EE platform, and it is welcomed by more and more developers who use ColdFusion and PHP. Although the original division of labor is no longer used, the idea of ​​division of labor is still used and is widely used in software engineering. It is a typical and widely used software architecture model. Later, the idea of ​​MVC was applied to the development of Web, known as the Web
MVC framework.

Two, MVC mode description

Insert picture description here

M is all spelled as Model, which mainly encapsulates access to the database layer, and performs operations on adding, deleting, modifying, and checking data in the database.
V is all spelled as View, which is used to encapsulate the result and generate the html content displayed on the page.
C is all spelled as Controller, which is used to receive requests, process business logic, interact with Model and View, and return results.

Three, Django's MVT

Insert picture description here

M is all spelled as Model, which has the same function as M in MVC, and is responsible for interacting with the database and processing data.
V is all spelled as View, which has the same function as C in MVC. It receives requests, processes business, and returns responses.
T is all spelled as Template, which has the same function as V in MVC, and is responsible for encapsulating and constructing the html to be returned.

Note: The difference lies in the part marked by the black line and black arrow

3. Django learning materials

Official website
Github source code
version 1.11 English document
version 1.11 Chinese document
Django Book tutorial
Tange With Django tutorial

Fourth, the virtual environment

1. Why build a virtual environment?

In the development process, when you need to use some toolkits/frameworks of python, you need to install it online,
such as installing the Django framework version 1.11.11 of Django online.

sudo pip install django==1.11.11

Tip: Using the above command, Django will be installed under the path /usr/local/lib/python2.7/dist-packages.
Problem: If you want to develop multiple different projects on one computer, you need to use the same package If you use the above command to install or update in the same directory, the
new version will overwrite the previous version, and other projects will not be able to run.

Solution: Virtual Environment
Function: The virtual environment can build an independent python operating environment, so that the operating environment of a single project does not affect other projects.
All virtual environments are located in a hidden directory under /home/.virtualenvs

2. How to build a virtual environment?

Command to install virtual environment:

sudo pip install virtualenv
sudo pip install virtualenvwrapper

After installing the virtual environment, if you are prompted that the mkvirtualenv command cannot be found, you must configure the environment variables:

# 1、创建目录用来存放虚拟环境
mkdir 
$HOME/.virtualenvs

# 2、打开~/.bashrc文件,并添加如下:
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

# 3、运行
source ~/.bashrc

Command to create a virtual environment:
Tip: If you do not specify the python version, the virtual environment
of python2 is installed by default. In python2, create a virtual environment

mkvirtualenv 虚拟环境名称
例 :
mkvirtualenv py_django

In python3, create a virtual environment

mkvirtualenv -p python3 虚拟环境名称
例 :
mkvirtualenv -p python3 py3_django

Insert picture description here
Tip:
Creating a virtual environment requires networking. After the
creation is successful, it will automatically work in this virtual environment. When
working in a virtual environment, the "virtual environment name" will appear at the top of the prompt

5. How to use the virtual environment?

View the command of the virtual environment:

workon

Insert picture description here
Commands for using virtual environments:

workon 虚拟环境名称

例 :使用py3_django的虚拟环境
workon py3_django

Insert picture description here
Command to exit the virtual environment:

deactivate

Insert picture description here
Command to delete virtual environment:

rmvirtualenv 虚拟环境名称

例 :删除虚拟环境py3_django

先退出:deactivate
再删除:rmvirtualenv py3_django

Insert picture description here

6. How to install the toolkit in a virtual environment?

Tip: Where to install the toolkit:

Under python2 version:
~/.virtualenvs/py_flask/lib/python2.7/site-packages/
Under python3 version:
~/.virtualenvs/py3_flask/lib/python3.5/site-packages

Install the django-1.11.11 package under the python3 version:

pip install 包名称

例 : 安装django-1.11.11的包
pip install django==1.11.11

Insert picture description here
View the packages installed in the virtual environment:

pip list

Insert picture description here

Seven, create a Django project

step

Create a Django project

  django-admin startproject name

Create sub application

 python manager.py startapp name

Create project

When using the Flask framework, the organization and creation of the project project directory needs to be manually created by ourselves.

In django, the project project directory can be created with the help of commands provided by django.

Create sub application

model

Site management

View and URL

Summarize the View and URL matching process

template

Summarize the View-Templates process

Display a list of books

Configuration and static files

Configuration file

Static file

App configuration

model

MVT diagram

Project preparation

Configuration

Define the model class

Shell tool and view MySQL database log

Database operations-add, delete, modify

Database operation-query

view

template

There are too many, the editor is still sorting out, if you want to learn in advance, you can pay attention to the editor and button up the skirt: 606115027 to get the document

Guess you like

Origin blog.csdn.net/weixin_45293202/article/details/113430966