程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Introduction and startup of Django framework

編輯:Python

List of articles

  • Preface
  • Django summary
  • Django install
  • Django Some common commands for
  • Django Introduction to some configuration files of
  • Django Running example of
  • Configure intranet access

Preface

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 summary

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

Django install

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 Some common commands for

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

Django Introduction to some configuration files of

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 

Django Running example of

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

Configure intranet access

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved