02、django中的上下文
生活随笔
收集整理的這篇文章主要介紹了
02、django中的上下文
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
1、譬如設置網站的名稱,setting中設置變量:
# setting.py SITE_NAME = "我的小站"2、在view中寫函數將該變量轉換成字典,做返回值
from django.conf import settings def site_key(request):# 這里使用的setting中的變量,也可以是在view中設置變量一樣的。return {"SITE_NAME":settings.SITE_NAME}3、在setting的上下文處理器中增加該函數的處理方法:
TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [os.path.join(BASE_DIR, 'templates')],'APP_DIRS': True,'OPTIONS': {'context_processors': ['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages','hydros.views.site_key', # view中的site_key函數],},}, ]4、直接在HTML中使用view中返回值的Key即可
<h2>{{ SITE_NAME }}</h2>轉載于:https://my.oschina.net/u/2474096/blog/511132
總結
以上是生活随笔為你收集整理的02、django中的上下文的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spark Mlib TFIDF源码详读
- 下一篇: 多线程的概述(一)