首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django上下文处理器没有Django的其余部分?

Django上下文处理器没有Django的其余部分?
EN

Stack Overflow用户
提问于 2017-05-26 03:31:19
回答 1查看 26关注 0票数 0

我正在尝试将Django模板作为独立的应用程序使用,但我在使用Engine.context_processors时遇到了问题

我的主文件是:

代码语言:javascript
复制
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中,我有:

代码语言:javascript
复制
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中,我有:

代码语言:javascript
复制
def my_context_processor(request):
    return {'abc': 'def'}

基本上,我的my_context_processor中的数据会被忽略...{{ abc }}未被替换。请参见上面代码的输出。我还打印了上下文:

代码语言:javascript
复制
[{'False': False, 'None': None, 'True': True}, {'var': 'Ricardo'}]
 -- HEADER ---
** Ricardo **
/tmp/filename_123.txt

 -- FOOTER ---

你知道为什么my_context_processor会被忽略吗?

EN

回答 1

Stack Overflow用户

发布于 2017-05-26 04:14:05

答案是:使用RequestContext而不是Context...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44188457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档