如何使用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 }}'/>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章