[英]My Django/Python Code is Not Rendering HTML <a> Tag in HTML Page
我想要一些類似於Twitter提及的內容,這些內容被轉換為鏈接,但它不起作用。 如果我們假設我們有message
='別忘了帶上Python書,@ friend'
#function to convert @mentions to links
def mentions(message, username):
this_user_handle = reverse('mysite:profile', args=[username])
new_message = re.sub(r'(@\w+)', r"<a href= '#'>\g<0></a>", message)
new_message.replace('#', this_user_handle)
return new_message
mentions(message, 'yax')
返回Do not forget to come with the Python book, <a href= '#'>@friend</a>'
。 #
未被替換, new_message
仍然顯示在HTML頁面中:
<p class= 'Post'>
{{ new_message|linebreaksbr}}
</p>
這顯示了這個:
Do not forget to come with the Python book, <a href= '#'>@friend</a>'
代替:
我該如何解決這個問題? 先感謝您!
內容正在自動轉義,以防止腳本注入等內容。 使用|safe
過濾器,你確定它不能包含任何令人討厭的東西。
使用kwargs而不是args:
reverse('profile', kwargs={'user_name': username})
你應該用href ='“替換href ='',以避免混合你不想混合的東西(你需要在幾個地方替換它)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.