300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > django配置templates static media和连接mysql数据库

django配置templates static media和连接mysql数据库

时间:2022-04-28 09:54:37

相关推荐

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等文件压缩'pressorFinder',)

# 项目一级路由urls.py配置,方便通过url访问 url(r'^static/(?P<path>.*)$', 'django.views.static.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访问url(r'^media/(?P<path>.*)/$', "django.views.static.serve", {"document_root": settings.MEDIA_ROOT}),

4.数据库配置

# =======数据库配置======DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql', # 数据库引擎'HOST': 'localhost', # 主机'USER': 'root', # 用户'PASSWORD': 'root', # 密码'PORT': '3306', # 端口号'NAME': 'mysite', # 数据库名}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。