Because the short term needs Django Frame to make a NLP The system is used to show , So record what you have learned , It is also used to get started quickly , Mostly from b From the station .
【 The most complete network 】 newest Django Complete course , Super detailed explanation
I think it was quite clear . Not enough. The version I use is different from him , I use it Django==3.2.13. However, no incompatibility has been found yet .
Django Is an open source Web Application framework , from Python It's written in . Adopted MTV Framework model of , The model M, View V And templates T.
Django Of MTV Pattern :
M The model layer : Responsible for interacting with the database
T Formwork layer : Responsible for rendering content to the browser
V View layer : Responsible for accepting requests 、 get data 、 Return results
The input terminal
pip install Django==3.2.13
If you use Pycharm The terminal should automatically activate the virtual environment , Just type the command directly , If it is VScode, If not configured , The terminal may not be able to be installed , At this time, you need to activate the virtual environment yourself
conda activate Virtual environment path
Sinister (TF2.1) Indicates that the terminal has been activated TF2.1 This virtual environment
Of course, it will be more troublesome if you think you have to activate it every time , Can be in VScode Of setting.json Configure automatic activation of virtual environment in
You can refer to my other blog
Hand to hand VScode The terminal is automatically activated anaconda Of python A virtual environment
After installation, you can check
pip show django
If you can see such information , explain Django Has been successfully installed .
Django-admin startproject file name # Create project
Python manage.py runserver Port number # Run the server
Ctrl+c # Exit server
Python manage.py startapp Application name # Create an
After creating the project and starting the server once , The file structure is as follows :
among db.sqlite3 It needs to be run once to generate , You can tell from the name that it is related to the database .
__ init __:python Package initialization file
Wsgi.py:web Configuration file of service gateway , The official start-up requires
Urls.py: Main route configuration of the project
Setting.py: The configuration file for the project
Setting.py contain django All configuration items started by the project , It is divided into public configuration and user-defined configuration
Example :BASE_DIR( Capitalization ) = “xxxx”
Public configuration :Django The official basic configuration
BASE_DIR = Path(__file__).resolve().parent.parent # Returns the absolute path of the project
DEBUG = True # Debug mode , It can prompt errors and restart immediately after code update
ALLOWED_HOSTS = [] # Used for filtration HOST Value of the header of , Default configuration is 127.0.0.1
LANGUAGE_CODE = 'zh-Hans' # Setup language
TIME_ZONE = 'UTC' # Setup time
First open the terminal
django-admin startproject demo // Create a name demo The file of
You can see that the project is created .
Then go to the project folder Directory
cd demo
Then you can run the server
python manage.py runserver
django The default address is 127.0.0.1
Finally, open the browser and see this page , It indicates that it has been started successfully
A simple understanding is that together with a WiFi You can access .
First
Input... In the terminal ipconfig Find your own ip Address
ipconfig
I'm currently ip The address is 192.168.56.1
stay setting.py Of ALLOWED_HOSTS Join in ip
ALLOWED_HOSTS = ["192.168.56.1"]
Then run on the command line
Cmd function python manage.py runserver 0.0.0.0:5000
Then enter... On the web page 192.168.56.1:5000 You can go to the website