【问题标题】:django escape tag not escaping single quotation marksdjango转义标签不转义单引号
【发布时间】:2016-01-08 15:58:45
【问题描述】:

我正在使用 django 1.7。

当用户输入单引号和/或双引号作为其输入的一部分时,显示其输入数据的表单会损坏。

所以我使用了 django 的 escape 标签,它应该很容易处理这个问题。

但是,escape 标签只是转义了双引号。单引号没有被转义,正在破坏测试表单。

这是我的代码示例:

{{ field|escape }}

有谁知道如何解决这个问题。

编辑

这是我的表单域模板代码(form_fields.html):

<div id="row_{{ field.auto_id }}" class="form-group {% if field.errors %}error{% endif %} {% if hide_row %}hidden{% endif %}">
    <label for="{{ field.auto_id }}" class="control-label {{ field.css_classes }}">
        {{ field.label }}{% if field.label %}:{% endif %}
    </label>
    <div class="controls {{ control_classes }}">
        {{ field|escape }}
        {% if field.errors %}
            <span class="help-inline">
                <strong>
                    {% for e in field.errors %}
                        {{ e }}<br/>
                    {% endfor %}
                </strong>
            </span>
        {% endif %}
        {% if field.help_text %}
            <p class="help-block">
                {{ field.help_text }}
            </p>
        {% endif %}
    </div>
</div>

这是表单模板字段代码:

{% load form_fields %}
....
{% form_field form.name_details_prefix_title %}

【问题讨论】:

  • 你能提供一些例子吗?您的表单、字段和一些模板代码可以重新创建吗?
  • GwybBleidD - 按要求添加代码。
  • 你不能使用escape过滤器来逃避整个字段,你应该只用价值来做,但这应该是自动的。请显示您的表单类。

标签: django django-templates escaping


【解决方案1】:

我最终追踪到了一个 js 问题。

我使用以下标签解决了这个问题:

{{ field|escapejs }}

希望这对某人有所帮助。

【讨论】:

  • 帮了我很多。正在为一个单引号而苦苦挣扎。非常感谢
猜你喜欢
  • 2018-12-17
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
  • 2023-04-02
  • 2017-07-24
相关资源
最近更新 更多