2017-05-25 57 views
0

我試圖使用Django模板作爲一個獨立的應用程序,我在與Engine.context_processorsDjango上下文處理器沒有Django的其餘部分?

我的主要文件的問題是:

from django.template import Template, Context, Engine 

template_content = """ -- HEADER --- 
{{ var|star_wrap }} 
{% fullpath "filename_123.txt" %} 
{{ abc }} 
-- FOOTER ---""" 

data = {'var': 'Ricardo'} 

engine = Engine(
    debug=True, 
    builtins=['filters'], 
    context_processors=(
     'context_processors.my_context_processor' 
    ) 
)  
template = Template(
    template_content, 
    engine=engine, 
) 
context = Context(data)  
result = template.render(context) 

在我filters.py我:

from django import template 

# --- Filters 
register = template.Library() # pylint: disable=C0103 

@register.filter(name='star_wrap') 
def star_wrap(value): 
    return "** " + value + " **" 

@register.simple_tag(takes_context=True) 
def fullpath(context, arg): 
    print(context) 
    return "/tmp/"+str(arg) 

而在context_processors.py我有:

def my_context_processor(request): 
    return {'abc': 'def'} 

基本上我的my_context_processor中的數據被忽略... {{ abc }}未被替換。查看上面代碼的輸出。我還打印方面:

[{'False': False, 'None': None, 'True': True}, {'var': 'Ricardo'}] 
-- HEADER --- 
** Ricardo ** 
/tmp/filename_123.txt 

-- FOOTER --- 

任何想法,爲什麼my_context_processor被忽略?

回答

0

答案是:用RequestContext代替Context ...

相關問題