SoFunction
Updated on 2024-12-19

An article that takes you to teach yourself python Django

1. Django Introduction

What is Django?

Django is developed in Python is a free and open source Web framework can be used to quickly build high-performance , elegant Web site ! Adopted MVC-> MVT framework pattern.

Django Outlook

In Python, there are 13045 packages related to web development, among which django accounts for 9091, about 70%. Meanwhile, there are as many as 54 active web frameworks in Python, the most active of which include Django, flask, etc. Django is one of the leaders, up to now, there are more than 5200 medium and large-scale websites using Django development. Some of the more well-known companies that use Django:

Domestic: Douban, Zhihu, etc.

Overseas: Google, YouTube (tubes), Instagram (photo wall) ->facebook, etc.

Django Framework Core

1. An object-oriented mapper used as a medium between a data model (defined as a Python class) and a relational database;

2., a regular expression-based URL distributor (route distributor);

3. A view system for processing requests;

4. A template system (HTML, css, js).

2. Design patterns

MVT mode

Django's MVT design pattern consists of Model, View and Template, which correspond to a single app directory, and templates folder. They may not seem consistent with the MVC design pattern, but in fact the essence is the same.Django's MVT design pattern and the classic MVC corresponds to the following.

  • Django Model: similar to the model Model in MVC
  • Django View (view): similar to the MVC controller Controller, is responsible for reading data from the database according to the user request , specify the way to y the user to display data ( web page or json data ) , but also can handle the data submitted by the user .
  • Django Template: Similar to a View in MVC, the View is used to render the data passed by Django View and also determines the appearance of the user interface. It also contains forms, which are used to collect the data passed by the user.

Django MVT design pattern in the most important is the view (view), view will be at the same time with the model (model) and templates (templates) for interaction. When the user sends a request (request), Django will parse the request header information , analyze the user needs to access the url address , and then according to the route defined in the corresponding relationship between the request forwarded to the corresponding view processing . The view will read the required data from the database, specify the rendering template, and finally return the response data.

在这里插入图片描述

3. Introduction to the development environment

1. System support: linux (recommended, project commonly used), mac, windows

unix

Recommended for linux: linux compatibility is better than mac and windows.

systems

  • Installation of linux in a virtual machine (recommended)
  • Using Cloud Servers
  • Installation of dual systems

3. Development environment support

  • python
  • pip
  • virtualenv (virtual environment)
  • mysql

Note: Why you need to be in a virtual environment when developing:

4. clean, environment and environment are independent of each other. eg: crawler project in environment 1, Django in environment 2, then the use of packages in the two projects will not conflict with each other.

4. Create a virtual environment

4.1. First install the management environment packages and the virtual environment packages

4.1.1 Installing the environment management package virtualenvwrapper

在这里插入图片描述

Note: The terminal for pycharm's editor is the same as the cmd terminal, so installing in pycharm is the same operation

4.1.2 Installing the virtual environment package virtualenv

在这里插入图片描述

4.1.3 Check pip list for successful installation

在这里插入图片描述

Checked for the presence of the above two packages, so the installation was successful.

4.2 Operation of the virtual environment

4.2.1 Creating a virtual environment

The command to create a virtual environment ismkvirtualenv Creating an environment name

在这里插入图片描述

(django2108_1) indicates that it has entered the environment.

4.2.2 Viewing packages in the virtual environment and installing Django packages

在这里插入图片描述

4.2.3 Exiting the virtual environment

The order is:deactivate

在这里插入图片描述

4.2.4 Viewing the Creation of All Virtual Environments

The order is:workon

在这里插入图片描述

4.2.5 Entering a Specified Virtual Environment

The order is:workon Created virtual environments

在这里插入图片描述

4.2.6 Deleting a virtual environment

The order is:rmvirtualenv virtual environment name

Note: To delete a virtual environment, first log out of the virtual environment you want to delete.

在这里插入图片描述

4.3 Replacing the path in the virtual environment (the path to create the virtual environment is on the c drive by default)

在这里插入图片描述

Depending on your choice, store the environment variables to different paths by creating the system variable WORKON_HOME.

5. Project creation and service startup

5.1 Creating Projects

5.1.1 Creating the virtual environment again

在这里插入图片描述

5.1.2 Creating folders on disk to hold projects

在这里插入图片描述

5.1.3 Opening the folder where the project is stored as a project file

在这里插入图片描述

5.1.4 Adding environment variables

在这里插入图片描述

5.1.5 Installing django packages in a new environment

在这里插入图片描述

5.1.6 Creating projects

The command is: django-admin startproject Project name

在这里插入图片描述

5.1.7 Creating sub-applications

在这里插入图片描述

First cd into the path of the created project path, and second enter the python startapp subapp name to create the subapp.

5.1.8 Registering subapplications

在这里插入图片描述

5.2 Starting services

Way 1: Enter the command python runserver directly into the terminal

在这里插入图片描述

Way 2: By setting the terminal execution parameters in Python

在这里插入图片描述

Way three: by setting up Django server (recommended), note: you need to set up the root directory and configuration file

在这里插入图片描述

5.3. Open the web page and view the results

在这里插入图片描述

summarize

That's all for this post, I hope it helped you and I hope you'll check back for more from me!