我有使用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标签?
答案 0 :(得分:1)
试试这个:
{% filter escape %} msg {% endfilter %}
或者您可能需要使用force_escape过滤器,具体取决于具体情况
答案 1 :(得分:0)
我没有完全跟随你,但你试过吗
{{ msg|safe }}
答案 2 :(得分:0)
似乎你想把你的html内容放在一些变量中,并且不希望它在模板中被转义并希望它被用作普通的html。在那种情况下,
msg = "<p>Location</p>"
{% autoescape off %}{{msg}}{% endautoescape %}
答案 3 :(得分:0)
如果您的转义文本来自模型中的字段(或来自后端的其他方式),您可以(除了人们已经说过的内容)在您的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 }}