【django】全局上下文
生活随笔
收集整理的這篇文章主要介紹了
【django】全局上下文
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
全局上下?是整站共享的上下?數據。
一、實現步驟
1、 在應?包下創建xxx.py?件
2、 編輯xxx.py?件
3、配置?件中添加?定義全局上下?
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',#此處添加新的全局上下文'film.mycontext.getsname'],},}, ]4、模板??直接引?
路由:
urlpatterns = [path('mcontext/',views.mcontext) #全局上下文 ]自定義的上下文:
def getsname(request):return {'sname':'zilv'}視圖:
def mcontext(request):return render(request,'film/index.html')模板:
<!DOCTYPE html> <html lang="en"><head> <meta charset="UTF-8"><title>Title</title></head> <body>{{ sname }} </body> </html>運行結果:
總結
以上是生活随笔為你收集整理的【django】全局上下文的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UnboundLocalError: l
- 下一篇: 【django】自定义中间件