gpt4 book ai didi

django - 使用 csrf token 和模板上下文渲染模板

转载 作者:行者123 更新时间:2023-12-02 03:50:42 33 4
gpt4 key购买 nike

目前我正在使用以下语法渲染 HTML View :

t = loader.get_template('sometemplate.html')
c = Context ({
'title': title,
'content': conent,
})
return HttpResponse(t.render(c))

我想添加一个具有 CSRF 保护的表单。我应该在语法中更改哪些内容,以便可以传递上下文和 token 来渲染 View ?

Django docs显示了渲染带有 CSRF token 的 View 的不同方法,但这些示例中没有传递其他上下文。

提前致谢!

最佳答案

使用RequestContext代替Context?另外,您可以只使用 render_to_response 而不是返回 HttpResponse:

from django.shortcuts import render_to_response
from django.template import RequestContext

return render_to_response('sometemplate.html', { 'title': title, 'content': content }, context_instance=RequestContext(request))

关于django - 使用 csrf token 和模板上下文渲染模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5282483/

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