django配置templates、static、media和连接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訪問 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', # 數據庫名} }?
轉載于:https://www.cnblogs.com/konglingxi/p/9406757.html
總結
以上是生活随笔為你收集整理的django配置templates、static、media和连接mysql数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 【Telerik】<telerik
- 下一篇: Assigned 的迷思
