【问题标题】:Default RequestContext in DjangoDjango 中的默认请求上下文
【发布时间】:2014-10-30 14:19:56
【问题描述】:

我有这样的功能(视图)

def index(request):
    return render_to_response('index.html', context_instance=RequestContext(request))

只想写

return render_to_response('index.html')

我还想传递额外的变量来查看

return render_to_response('cart.html', {'key': value})

我需要 RequestContext 的主要原因是我有上下文处理器功能,可以为我设置额外的变量。我怎样才能做到这一点,或者有不同的方法来做这样的事情?

【问题讨论】:

    标签: python django templates httprequest django-context


    【解决方案1】:

    您可以使用render 快捷方式:

    return render(request, 'cart.html', {'key': value})
    

    不过,您总是需要传递请求:这就是它被称为 RequestContext 的原因。

    猜你喜欢
    • 2019-07-28
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多