gpt4 book ai didi

django - 如何在 Django 1.8 的 render_to_response 中使用 RequestContexts

转载 作者:行者123 更新时间:2023-12-02 04:50:07 26 4
gpt4 key购买 nike

我知道 Django 1.8 中的 render_to_response 发生了变化接受论据。我有很多观点,到处都使用这种模式:

...return render_to_response( template, context, context_instance=MyRequestContext(request))

MyRequestContext 扩展了 RequestContext 并添加了一些我经常在模板中使用的参数。

现在这不再有效,MyRequestContext 中的值不再可以在模板中访问。

那么现在在Django 1.8中应该如何使用RequestContext呢?我需要它们 + 一些上下文传递给我的所有模板。

/J

-- 编辑--

感谢所有的回答,正如您指出的这应该有效...

我最终重写并替换了我之前的 RequestContext 子类 --> MyRequestContext w.执行单独的 ContextProcessors 函数并将其添加到我的 OPTIONS:context_processors 列表中,然后在我的所有 View 中使用普通的 RequestContext(request) 。不确定我在旧解决方案中遇到了什么错误/问题,但现在可以了。再次感谢您的启发和回应。

最佳答案

您可以手动获取模板并将其呈现给 HttpResponse 对象:

from django.template.loader import get_template
from django.http import HttpResponse

template = get_template(template_name)
context = MyRequestContext(request, context_dict)
return HttpResponse(template.render(context))

关于django - 如何在 Django 1.8 的 render_to_response 中使用 RequestContexts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29500722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com