【问题标题】:How to csrf_token protection in jinja2 template engine?如何在 jinja2 模板引擎中进行 csrf_token 保护?
【发布时间】:2011-12-12 06:32:37
【问题描述】:

在我使用的 Django 模板中:

<form action="/user" method="post">{% csrf_token %}
    {{ form.as_p|safe }}
    <input type="submit" value="Submit" />
</form>

但是当我更改为jinja2 template engine时出错:

 Encountered unknown tag 'csrf_token'

我的问题:jinja2 中的csrf_token protection 是必需的吗?

如果需要,怎么做?

提前致谢!

【问题讨论】:

    标签: django csrf jinja2


    【解决方案1】:

    看起来 Jinja2 的工作方式有所不同:

    使用&lt;input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}"&gt; 在 Django 模板中你使用{% csrf_token %}

    来源:http://exyr.org/2010/Jinja-in-Django/

    【讨论】:

    • 这个也适用于 django-jinja python 库。 (一个帮助库,使过渡到 Jinja2 变得轻而易举)。
    • {{ csrf_input }} 是一个较短的替代方案,如@Isowen's answer中所述
    【解决方案2】:

    我遇到了同样的问题,我注意到 CSRF 上下文处理器不在默认加载的处理器列表中。将'django.core.context_processors.csrf'添加到setting.py中的TEMPLATE_CONTEXT_PROCESSORS后,我可以正常使用{% csrf_token %}模板标签。

    【讨论】:

    • 他说的是 jinja2,而不是 django 模板
    【解决方案3】:

    我使用Coffin。 使用时遇到同样的问题:

    from coffin.shortcuts import render_to_response
    return render_to_response('template_name_here.html', context)
    

    尝试改用:

    from coffin.shortcuts import render
    return render(request, 'template_name_here.html', context)
    

    【讨论】:

      【解决方案4】:

      我知道这是一个老问题,但是当使用 Django 1.8+ 中可用的新 django.template.backends.jinja2.Jinja2 时,我想以正确的方式更新它以支持 csrf_token。使用 django 模板后端,您会调用 {% csrf_token %},但使用 Jinja2 后端,您将使用 {{ csrf_input }} 调用它(您可以使用 {{ csrf_token }} 获取令牌值而不是令牌输入)。

      可以在django.template.backends.jinja2.Jinja2source查看详情

      【讨论】:

      • 当使用宏来渲染表单时,要传递csrf_input ,您需要将它们与上下文一起导入,例如{% from "bootstrap/forms/horizontal.html" import render_form with context%}。请参阅Jinja2 import visibility 了解更多信息。
      【解决方案5】:

      在带有 jinja2 模板引擎的 django 2.x 中,您可以使用 {{ csrf_token }} 获取令牌的值,并使用 {{ csrf_input }} 获取完整的隐藏输入标记

      来源:https://django.readthedocs.io/en/2.1.x/ref/csrf.html

      示例:

      <form action="..." method="post">
        {{ csrf_input }}
      
         ...
      </form>
      

      【讨论】:

      【解决方案6】:

      您不再需要做任何特别的事情。 django-jinja 支持 csrf_token 并且开箱即用。

      <!DOCTYPE html>
      <html>
        <head>
          <meta charset="utf-8"/>
          <title>test</title>
        </head>
        <body>
          <p>This should add a hidden input tag with the token. use it in your forms</p>
          {% csrf_token %}
        </body>
      </html>
      

      【讨论】:

      • 当我以这种方式使用它时,我得到了("Encountered unknown tag 'csrf_token'. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'.",) 错误。
      • 这很奇怪。我正在使用它。:
        {% csrf_token %} {{ form.as_p() }} ...您是否在 settings.py 中正确列出了 django_jinja.builtins.extensions.CsrfExtension ,在模板部分?
      猜你喜欢
      • 2018-07-15
      • 2015-08-22
      • 2018-05-04
      • 2014-03-24
      • 2016-08-23
      • 1970-01-01
      • 2014-03-15
      • 2012-08-02
      • 2017-11-09
      相关资源
      最近更新 更多