[英]Django, exclude a context processor in a view
我有一個帶有菜單的網站。 要生成菜單,我需要在數據庫中進行一些查詢。 因此,我創建了一個上下文處理器以在所有視圖中進行操作。
我的某些觀點實際上是形式。 當用戶單擊某些按鈕時,我使用ajax獲取它們,並使用jquery ui對話框顯示它們。
對於那些非常復雜的形式,我無法刪除所有上下文處理器,尤其需要auth,static和il8n上下文處理器。 但是我不想在數據庫中進行基於菜單的查詢來顯示這些表格。
有沒有辦法在視圖中排除上下文處理器? 我試圖在視圖的“ request.session”中放入一個變量,然后將其刪除並在上下文處理器中返回一個空字典。 但這很糟糕,並且可能存在並發問題。 我還可以在上下文處理器中的“請求”中解析網址,然后返回一個空字典,但這聽起來又像是一種黑客。
有什么想法或建議嗎? 謝謝,
你可以繼承RequestContext
在django.template.context
並重新定義其__init__
方法。 然后,您可以在那些特定的視圖中使用經過修改的RequestContext
。 RequestContext
的__init__
當前看起來像這樣:
def __init__(self, request, dict=None, processors=None, current_app=None, use_l10n=None): Context.__init__(self, dict, current_app=current_app, use_l10n=use_l10n) if processors is None: processors = () else: processors = tuple(processors) for processor in get_standard_processors() + processors: self.update(processor(request))
在這里, get_standard_processors()
返回設置中定義的上下文處理器。 在調用上下文處理器之前(上面代碼的最后一行),您可以添加檢查以確定是否需要使用哪些處理器,以及需要跳過哪些處理器。
重新設計您的應用程序可能會更容易,以便某些視圖進行此查詢,而其他視圖則不會。 您可以通過編寫一個“基於類的視圖”來進行此特定數據庫查詢,並將其添加到上下文中,然后在需要新視圖進行該額外查詢時繼承它,從而避免這種重復。 我主張使用這種方法,而不要使用全局上下文處理器。
本示例說明如何向默認模板上下文添加內容。
使用各種模板引擎非常容易實現。
TEMPLATES = [
# The default engine - a heavy one with a lot of context processors
{
'NAME': 'default',
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.template.context_processors.static',
'some_app_1.context_processors.very_heavy_cp_1',
'some_app_2.context_processors.very_heavy_cp_2',
'some_app_3.context_processors.very_heavy_cp_3',
],
'debug': True,
},
},
# Light engine - only very necessary things go here
{
'NAME': 'light',
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.template.context_processors.static',
],
'debug': True,
},
},
]
some_app_1.views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.generic import View, TemplateView
樣品燈視圖:
class TestLightView(View):
"""Test light view."""
template_name = 'some_app_1/test_light_view.html'
def get(self, request):
context = {}
response = render_to_response(
self.template_name,
context,
context_instance=RequestContext(request),
using='light' # Note, that we use `light` engine here
)
return response
樣本普通(重)視圖:
class TestNormalView(View):
"""Test normal view."""
template_name = 'some_app_1/test_normal_view.html'
def get(self, request):
context = {}
response = render_to_response(
self.template_name,
context,
context_instance=RequestContext(request),
using='default' # Note, that we use `default` engine here
)
return response
如果使用TemplateView
,請定義template_engine
屬性。
樣品燈視圖:
class TestTemplateLightView(TemplateView):
"""Test template light view"""
template_engine = 'light' # Note, that we use `light` engine here
# Your code goes here
樣本普通(重)視圖:
class TestTemplateNormalView(TemplateView):
"""Test template normal view."""
template_engine = 'default' # Note, that we use `default` engine here
# Your code goes here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.