【发布时间】:2012-03-29 20:30:17
【问题描述】:
我试图在我的自定义模板标签中创建一个表单,然后尝试在那里创建一个 HTML 表单,然后在我的模板中简单地使用它,但是当用户提交时我收到 CSRF 验证失败错误表单(这很自然,因为我没有在模板标签内提供带有 csrf_token 的表单),我这样做了:
@register.simple_tag(takes_context=True)
def change_password_(context):
csrf = context.get('csrf_token', '')
html = "<form action='{% url change_password %}' method='post' id='renew_password' >"
csrf_string = str(csrf)
html += "<input type='hidden' value='%' />" %csrf_string
....
但我仍然收到 CSRF 验证失败错误 :( 知道如何使它工作吗?
谢谢
【问题讨论】:
标签: python django templates csrf