【问题标题】:escape html tags转义 html 标签
【发布时间】:2012-04-21 20:42:51
【问题描述】:

我在使用 autoescape django 模板标签时遇到问题,这是我的代码

msg="<p><b>Location. </b> <br />Marriott Dallas Addison Quorum Galleria is located in Dallas's Addison - Galleria neighborhood, close to Galleria Dallas, Cavanaugh Flight Museum, and Northwood Club. Additional area points of interest include University of Texas at Dallas. </p><p><b>Hotel Features. </b><br />Dining options at Marriott Dallas Addison Quorum Galleria include a restaurant and a coffee shop/café. A bar/lounge is open for drinks. Room service is available. The hotel serves buffet breakfasts (surcharges apply). Recreational amenities include an outdoor pool, an indoor pool, and a spa tub. This 3.5-star property has a business center and offers small meeting rooms, limo/town car service, and audio-visual equipment. Wireless Internet access (surcharge) is available in public areas. The property offers a roundtrip airport shuttle (surcharge).  Guest parking is available for a surcharge. Additional property amenities include a concierge desk, gift shops/newsstands, and ATM/banking services. </p><p><b>Guestrooms. </b> <br /> 547 air-conditioned guestrooms at Marriott Dallas Addison Quorum Galleria feature coffee/tea makers and complimentary newspapers. Wired high-speed and wireless Internet access is available for a surcharge. In addition to desks, guestrooms offer multi-line phones with voice mail. Televisions have premium cable channels and pay movies. Rooms also include hair dryers and irons/ironing boards. A turndown service is available nightly, housekeeping is offered daily, and guests may request wake-up calls. </p> <br /><br /> <p><strong>Notifications and Fees:</strong><br /></p><p></p><p></p><p></p><p>The following fees and deposits are charged by the property at time of service, check-in, or check-out.  <ul><li>Self parking fee: USD 12 per day</li><li>Valet parking fee: USD 17 per day</li><li>Fee for wireless Internet in public areas: USD 3.95 per day (rates may vary)</li> <li>Fee for in-room high-speed Internet (wired): USD 12.95 per day (rates may vary)</li><li>Fee for in-room wireless Internet: USD 12.95 per day (rates may vary)</li><li>Buffet breakfast fee: USD 17 per person (approximate amount)</li> </ul></p><p>The above list may not be comprehensive. Fees and deposits may not include tax and are subject to change. </p> <br /><br /> <p><strong>Notifications and Fees:</strong><br /></p><p></p><p></p><p></p><p>The following fees and deposits are charged by the property at time of service, check-in, or check-out.  <ul><li>Self parking fee: USD 12 per day</li><li>Valet parking fee: USD 17 per day</li><li>Fee for wireless Internet in public areas: USD 3.95 per day (rates may vary)</li> <li>Fee for in-room high-speed Internet (wired): USD 12.95 per day (rates may vary)</li><li>Fee for in-room wireless Internet: USD 12.95 per day (rates may vary)</li><li>Buffet breakfast fee: USD 17 per person (approximate amount)</li> </ul></p><p>The above list may not be comprehensive. Fees and deposits may not include tax and are subject to change. </p>"

{% autoescape off %}{{msg}}{% endautoescape %} 

但它仍然显示 html 标签?

【问题讨论】:

  • 那不是“代码”。 msg= 行是什么意思?我想这是模板变量的假定字符串值? “显示 html 标签”是什么意思?它是如何出现的?您希望它如何显示?

标签: python django django-templates


【解决方案1】:

试试这个:

{% filter escape %} msg {% endfilter %}

或者你可能需要使用 force_escape 过滤器,视情况而定

【讨论】:

    【解决方案2】:

    我不完全关注你,但你有没有尝试过

    {{ msg|safe }}
    

    【讨论】:

      【解决方案3】:

      似乎您想将您的 html 内容放在某个变量中,并且不希望它在模板中转义并希望它用作普通 html。在这种情况下,

      msg = "<p>Location</p>"
      {% autoescape off %}{{msg}}{% endautoescape %}
      

      【讨论】:

        【解决方案4】:

        如果您的转义文本来自模型中的字段(或以其他方式来自后端),您可以(除了人们已经说过的)在 Python 代码中使用 mark_safe,而不是做任何事情在模板中。见这里:https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.safestring.mark_safe

        假设模型中的字段始终填充有将自动转义的字符:您可能希望向模型添加一个方法,该方法返回标记为安全的值。例如:

        from django.utils.safestring import mark_safe
        
        class Something(models.Model):
            html_text = models.CharField(max_length=30)
            #...
        
            def get_html(self):
                return mark_safe(self.html_text)
        

        然后在你的模板中你只需使用{{ my_something.get_html }}

        【讨论】:

          猜你喜欢
          • 2010-10-16
          • 2016-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-30
          • 2023-03-24
          • 2017-12-18
          相关资源
          最近更新 更多