2013-08-22 78 views
0

我想要一個HTML序列在HTML中呈現,而不是以字符串形式顯示。將一個httpresponse對象渲染爲html

Views.py -

from django.utils.html import escape 
ret_html = """<p> Please click the following link 
to authorize the application - <a href='""" + auth_url + """' target='_blank'>Authorize!</a> </p>""" 
      return HttpResponse(escape(ret_html)) 

以上被瀏覽器中呈現完全一樣 -

<p> Please click the following link to authorize the application - <a href='/social/addtwitter/' target='_blank'>Authorize!</a> </p> 

而我希望它是這個樣子 -

請點擊以下鏈接授權應用程序 - [授權] [1]

任何想法,我可以做一些調整,以呈現字符串爲HTML。

PS:我知道renderrender_to_response

+0

您需要將其標記爲安全:https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.html.es海角 –

回答

0

你需要在模板中使用|safe filter

或者只是

return HttpResponse(safe(escape(ret_html))) 

建議使用renderrender_to_response

+0

剛剛返回'mark_safe(ret_html)'幫助!! – user1629366