2012-04-09 142 views
-2

我有問題,在這裏使用autoescape Django的模板標籤是我的代碼逃生html標籤

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

這不是「代碼」。那個'msg ='行是什麼意思?我想這是模板變量的假定字符串值?你是什​​麼意思「顯示HTML標籤」?它是如何出現的?你想如何出現? – 2012-04-09 14:06:56

回答

1

試試這個:

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

,或者您可能需要使用force_escape過濾器,根據不同的情況

0

我不完全跟着你,但你試過

{{ msg|safe }} 
0

似乎你想把你的HTML內容放在一些變量中,並且不希望它在模板中轉義,並且希望它被用作普通的html。在這種情況下,

msg = "<p>Location</p>" 
{% autoescape off %}{{msg}}{% endautoescape %} 
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 }}