如何在Django视图装饰器的上下文中设置值?

2024-09-30 02:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想编写一个decorator,它将为模板呈现上下文提供值,如下所示:

@deco(art=Nouveau)
def my_wall(request):
    # should have art == 'something' in the template
    return render_from_template('plain_white.html', {art:'something'}, context_instance=RequestContext(request))

@deco(art=Nouveau)
def my_wall(request):
    # should have art == Nouveau in the template
    return render_from_template('plain_white.html', {}, context_instance=RequestContext(request))

我最初的尝试是这样的:

^{pr2}$

但是,这不起作用,因为请求不是真正的dict

我如何实现这一点?在


Tags: theinreturnrequestmydefhavetemplate
1条回答
网友
1楼 · 发布于 2024-09-30 02:29:36

唯一的方法是如果decorator本身负责呈现和返回模板。有一个相当成熟的代码片段正是这样做的:render_to装饰器。其思想是视图只返回一个字典,而decorator实际呈现它。您应该能够编辑该片段,以便它处理默认情况。在

相关问题 更多 >

    热门问题