-2

我在使用 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标签?

4

4 回答 4

1

尝试这个:

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

或者您可能需要使用 force_escape 过滤器,具体取决于情况

于 2012-04-09T13:35:45.660 回答
0

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

msg = "<p>Location</p>"
{% autoescape off %}{{msg}}{% endautoescape %}
于 2013-04-02T18:27:04.737 回答
0

我不完全跟随你,但你试过吗

{{ msg|safe }}
于 2012-04-11T03:31:53.150 回答
0

如果您的转义文本来自模型中的字段(或以另一种方式来自后端),您可以(除了人们已经说过的)mark_safe在 Python 代码中使用,而不是在模板中做任何事情。见这里: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 }}

于 2013-04-16T04:13:28.683 回答