Django study notes -2

Basic configuration information

Configuration information is mainly realized by the project settings.py file, main configuration:

  • Project Path

  • Key configuration

  • Domain access

  • App List

  • Configuring static resources

  • Configuration template file

  • Database Configuration

  • Middleware

  • Cache configuration

     settings.py的基本配置如下:
    

    Here Insert Picture Description

     上述代码列出了项目路径BASE_DIR、密钥配置SECRET_KEY、调试模式DEBUG、访问权限ALLOWED_HOSTS和App列表INSTALLED_APPS,各个配置说明如下:
     
     ● 项目路径BASE_DIR:        主要通过os模块读取当前项目在系统的具体路径,代码在创建项目时自动生成,通常无需修改。
     
     ● 密钥配置SECURIT_KEY:     是一个随机值,在项目创建的时候自动生成,通常无需修改。主要用于重要数据的加密处理,提高系统安全性。
     
     ● 调试模式DEBUG:           该值为布尔类型。开发局阶段True,项目部署上线改为False。
     
     ● 域名访问权限ALLOWED_HOSTS:设置可访问的域名,默认为空。DEBUG=True同时ALLOWD_HOSTS为空代表项目只允许以localhost或者127.0.0.1在浏览器上访问。DEBUG=False时ALLOWD_HOSTS为必填项,设置为ALLOWD_HOSTS=['xxx.xxx.xxx.xxx']
     
     App列表INSTALLED_APPS,告诉django有哪些App,项目中已有配置信息如下:
    
name Explanation
admin Built-in back-end management system
auth Built-in user authentication system
contenttypes model metadata records project
sessions Session Session function for user identity information to access the site
messages Message prompts
staticfiles Find a static resource path
	如果项目中加入了App,需要在INSTALLED_APPS列表中加入App名字,如:

Here Insert Picture Description

Static resources

静态资源指,网站中不变的文件。静态资源包括CSS文件、JavaScript文件以及图片等资源文件。
	● CSS(层叠样式表)	一种用来表现HTML或XML文件样式的计算机语言。
	● JavaScript        一种直译式脚本语言在HTML网页上使用,用于给网页增加动态功能。
静态文件存放在配置文件settings.py文件上,如下:

Here Insert Picture Description

上述配置将静态资源存放在文件夹static,这个文件夹只能放在App里面。
项目启动时,Django根据静态资源存放路径查找相关文件,查找功能由App列表INSTALLED_APPS的staticfiles实现。
在index App中添加‘python package’并放置文件。

Here Insert Picture Description

启动项目后,浏览器访问:http://127.0.0.1:8000/static/FileName
如果想在djangoDemo的根目录下存放静态资源,可以在settings.py中设置STATICFILES_DIRS属性。
该属性以列表形式表示,设置方式如下:

Here Insert Picture Description

分别在根目录下创建文件夹public_static,在App(index)下创建index_static文件夹。

Here Insert Picture Description
Here Insert Picture Description

Module Path

模板是一种较为特殊的HTML文档。这个文档中嵌入了一些Python识别的变量和指令,然后程序解析这些变量和命令,生成完整的HTML网页并返回给用哦过户浏览。

MTV框架中的T。创建项目时,Django已初始化模板配置信息:
name Explanation
BACKEND Template engine for identifying variables and the instructions of the template
DIRS Set the path template, the template position to tell Django
APP_DIRS Find whether the template file in App file
OPTIONS RequestContex in the context of the calling function, usually without modification

Here Insert Picture Description

在项目根目录index下分别创建templates文件夹,并在文件夹下创建index.html和app_index.html

Here Insert Picture Description

根目录templates通常存放共用的模板文件,能够提供各个App的模板文件调用,该模板符合代码重复使用原则,如HTML的<head>部分。
index的templates是存放当前App所需的模板文件。模板配置如下:

Here Insert Picture Description

Database Configuration

选择项目所使用的数据库类型,不同数据库需要设置不同的数据库引擎,数据库引擎用于实现项目与数据库的连接,
Django提供四种数据库引擎:

Here Insert Picture Description

Django除了支持PostgreSQL、Sqlite3、MySQL和Oracle以外,还支持SQLServer和MongoDB的连接。

项目创建时,默认sqlite3数据库,这是一款轻型的数据库,常用于嵌入式系统开发,配置信息如下:

Here Insert Picture Description

如果把上述连接信息修改为MySQL数据库,首先安装MySQL连接模块‘mysqlclient’ ,输入命令:
    在线安装:pip install mysqlclient
    离线安装:pip install --no-index --find-links=file:E:\ mysqlclient-1.4.1-cp37-cp37m-win_amd64.whl

Here Insert Picture Description

 完成mysqlclient模块晚装后,配置settings.py的MySQL连接信息,如下
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190613234049604.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIxMTUzOTk3,size_16,color_FFFFFF,t_70)

Middleware

中间件是处理Django的request和response对象的过程。当用户在网站中进行某个按钮等操作时,这个动作是用户向网站发送request,
而网站根据操作返回相关内容,这个过程叫response。

Here Insert Picture Description

一般情况下,Django默认的中间件配置均可满足大部分的开发需求。在MIDDLEWARE中添加LocalMiddleware中间件,使得Django内置功能
支持中文显示,如下:

Here Insert Picture Description

中间件说明:
name Explanation
SecurityMiddleware Built-in security mechanisms to protect the user to communicate with the Site Security
SessionMiddleware Session Session function
CommonMiddleware Processing request information, request content standardization
CsrfViewMiddleware Open CSRF protection
AuthenticationMiddleware Open the built-in user authentication system
MessageMiddleware Turn on the built-in message alert feature
XFrameOptionsMiddleware Prevent malicious programs clickjacking
LocalMiddleware Chinese Language Support

Guess you like

Origin blog.csdn.net/qq_21153997/article/details/91909048