0
点赞
收藏
分享

微信扫一扫

如何使用render_to_string时填充{%csrf_token%}


我需要使用render_to_string,但不能使用

{%csrf_token%}

并通过

content = render_to_string('blog/comment.html', context=context)

return JsonResponse({'content': content})

解决方案

我已经解决了这个问题,但是我仍然非常想要一个好的解决方案。

面板功能现在看起来像:

def render_to_s(request, *args, **kwargs):
panelDisplays = PanelDisplay.listAll()
csrf_token_value = request.COOKIES['csrftoken']

c = {"panelDisplays": panelDisplays, "csrf_token_value": csrf_token_value}
return render_to_string('panels/config.html', c)

现在,模板包括:

<input type='hidden' name='csrfmiddlewaretoken' value='{{ csrf_token_value }}'/>

 

举报

相关推荐

0 条评论