2017-02-22 56 views
0

我希望我的用戶能夠編寫,例如image1並使用正則表達式來識別並用標籤替換它。我擁有了一切工作 - 但是當我把數據傳送到模板顯示我這樣做是這樣的:django render從views.py傳遞的html標籤

<div class="container"> 
     <p> 
      {% articleTxt %} 
     </p> 
    </div> 

ArticleTxt包含:

This is a test Article <img src="/media/articleImages/django-allauth.png"> 
and that was a picture and this is another one then some more text ---- 
original: This is a test Article *image1* and that was a picture and this is 
another one *image2* then some more text 

那是我的無奈。我試着改變模板:

{% autoescape off %} {{articleTXT}} {{% autoescape %}} 

我也試過

{{% articleTXT|safe %}} 

它總是打印標籤,而不是使它們的。無論如何,將標記嵌入從view.py傳遞到我的模板!

+0

我不理解'articleTXT'中的whats什麼?它的'img'或'text' – Bijoy

回答

1
{{ articleTXT |safe }} 

{% autoescape off %} {{ articleTXT }} {% endautoescape %} 

應該做的伎倆

*注:其{%endautoescape%}

如果心不是工作的嘗試,

format_html(articleTXT) 

在您的意見。

希望這會有所幫助:)

+0

format_html爲我工作!其餘的都沒有!所以解決方案是從django.utils.html導入format_html添加,然後使用processedText = format_html(processed_Text)謝謝你THANKYOU THANKYOU –

+0

真棒:D很高興它幫助。 –

相關問題