django 模板mysql_django配置templates、static、media和连接mysql数据库

1.模板文件

# =======templates配置=======

if os.path.exists(os.path.join(BASE_DIR, 'templates')) is False:

os.mkdir(os.path.join(BASE_DIR, 'templates'))

TEMPLATES = [

{

# 模板引擎,内置的模板引擎有:

# 1. 'django.template.backends.django.DjangoTemplates'

# 2. 'django.template.backends.JInJa2.JInja2'

# 你也可以使用非Django的模板引擎

'BACKEND': 'django.template.backends.django.DjangoTemplates',

# 引擎用于查找模板源文件的目录,按搜索顺序排列

'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, "mobile_app/dist"),

os.path.join(BASE_DIR, "big_screen/screenpro/dist"), ],

# 引擎是否在已经安装的应用程序的目录内查看模板源文件

'APP_DIRS': True,

# 传递给模板引擎(backend)的其他参数,不同引擎,可用的参数不一样

'OPTIONS': {

'context_processors': [

# 全局的processors,它默认是被传递给views中html模板的RequestContext对象

'django.template.context_processors.debug',

'django.template.context_processors.request',

'django.contrib.auth.context_processors.auth',

'django.contrib.messages.context_processors.messages',

# u"在模板变量中添加 {{ MEDIA_URL }}"

"django.template.context_processors.media",

# u"在模板变量中添加 {{ STATIC_URL }}"

"django.template.context_processors.static",

# 添加 移动标识 device(自定义)

"prod_core.template.context_processors.mobile",

],

},

},

]

2.静态文件

# ======static配置======

if os.path.exists(os.path.join(BASE_DIR, 'static')) is False:

os.mkdir(os.path.join(BASE_DIR, 'static'))

# 由templates配置中"django.template.context_processors.static"读取

# 可以在html模板上使用{{ STATIC_URL }}读取STATIC_URL

STATIC_URL = '/static/'

# python manage.py collectstatic 命令收集静态文件的目录,将各个app目录下的static收集于项目目录下的static中

STATIC_ROOT = 'static'

# 放各个app的static目录及公共的static目录

STATICFILES_DIRS = [

os.path.join(BASE_DIR, "mobile_app/dist/static/"),

os.path.join(BASE_DIR, "big_screen/screenpro/dist/static/")

]

STATICFILES_FINDERS = (

# 用来从 STATICFILES_DIRS 指定的路径中查找额外的静态文件

'django.contrib.staticfiles.finders.FileSystemFinder',

# 从 INSTALLED_APPS 列表内的 APP 所在包的 static 目录中查找资源文件

'django.contrib.staticfiles.finders.AppDirectoriesFinder',

# other finders.. css,js等文件压缩

'compressor.finders.CompressorFinder',

)

# 项目一级路由urls.py配置,方便通过url访问,实测不添加也可以访问

from django.views.static import serve

url(r'^static/(?P.*)$', serve, {'document_root': settings.STATIC_ROOT}),

3.媒体文件

# ======media配置======

if os.path.exists(os.path.join(BASE_DIR, 'media')) is False:

os.mkdir(os.path.join(BASE_DIR, 'media'))

# media目录,相当于设置媒体文件的绝对路径

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# 由templates配置的django.template.context_processors.media读取

# 在html模板上使用{{ MEDIA_URL }}读取媒体根目录

MEDIA_URL = '/media/'

# 项目一级路由urls.py配置,方便通过url访问

from django.views.static import serve

url(r'^media/(?P.*)/$', serve, {"document_root": settings.MEDIA_ROOT}),

4.数据库配置

# =======数据库配置======

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql', # 数据库引擎

'HOST': 'localhost', # 主机

'USER': 'root', # 用户

'PASSWORD': 'root', # 密码

'PORT': '3306', # 端口号

'NAME': 'mysite', # 数据库名

}

}

ps:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值