Django的訊息中內嵌HTML
Problem
You want to embed HTML within a message using Django's messages framework.
This is a reasonably common requirement - for instance, it's common to want to include a link within the message, perhaps pointing the user towards a sign-in or registration page.
This problem exists as of Django 1.4 but may be solved within the framework in later versions.
Solution
Use the extra_tags keyword argument to pass a flag indicating that the message is safe for rendering without escaping. For example:
2.
3.def some_view(request):
4....
5.messages.success(request,
6.'Here is a link.',
7.extra_tags='safe')Then use some simple template logic to determine whether to use the safe filter:
You want to embed HTML within a message using Django's messages framework.
This is a reasonably common requirement - for instance, it's common to want to include a link within the message, perhaps pointing the user towards a sign-in or registration page.
This problem exists as of Django 1.4 but may be solved within the framework in later versions.
Solution
Use the extra_tags keyword argument to pass a flag indicating that the message is safe for rendering without escaping. For example:
CODE:
1.from django.contrib import messages2.
3.def some_view(request):
4....
5.messages.success(request,
6.'Here is a link.',
7.extra_tags='safe')Then use some simple template logic to determine whether to use the safe filter:
CODE:
01.02.{% for message in messages %}
03.
10.{% endfor %}
11.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-734245/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- FolkMq v1.4.6 釋出(可以內嵌的訊息中介軟體)MQ
- 可以把 FolkMQ 內嵌到 SpringBoot3 專案裡(可內嵌的訊息中介軟體,純血國產)MQSpring Boot
- Django中的內建訊號有哪些Django
- android內嵌html5頁面不能播放影片AndroidHTML
- django+小程式傳送模板訊息Django
- dotnet OpenXML 讀取 PPT 內嵌 ole 格式 Excel 表格的資訊XMLExcel
- JDK中內嵌JS引擎介紹及使用JDKJS
- Nginx 內嵌變數Nginx變數
- Django搭建個人部落格:用django-notifications實現訊息通知Django
- Django中的templates(你的HTML頁面放哪裡)DjangoHTML
- MongoDB查詢內嵌文件MongoDB
- 訊息佇列中的Oracle佇列Oracle
- 使用iframe內嵌網頁的時候,如何做到內嵌網頁的高度自適應 有大用網頁
- 淺談小程式內嵌網頁及內嵌網頁跳轉分享實現網頁
- 內嵌樣式標記style
- 小程式內嵌h5H5
- Spring Boot 內嵌容器 Tomcat / UndertowSpring BootTomcat
- Oracle與MySQL內嵌遊標的使用示例OracleMySql
- RocketMQ中Producer訊息的傳送MQ
- Django搭建CMDB系統完整[5](base.html內容頁面)DjangoHTML
- 用程式碼理解 ObjC 中的傳送訊息和訊息轉發OBJ
- 訊息治理,到底需要治理哪些內容?
- SVG 立方體內嵌路徑拼接SVG
- 客戶端內嵌Vue頁面客戶端Vue
- Android/iOS內嵌Unity開發示例AndroidiOSUnity
- C++內嵌彙編 教程1C++
- Flutter中訊息傳遞Flutter
- SpringBoot原始碼解析-內嵌Tomcat容器的啟動Spring Boot原始碼Tomcat
- Netweaver的埠號和Spring boot內嵌的Tomcat埠Spring BootTomcat
- Objective-C中的訊息轉發Object
- Django中ORM找出內容不為空的資料DjangoORM
- 由外到內——剖析Android訊息機制Android
- 7月頭兩天內AI最新訊息AI
- 訊息推送介面設計(內含原始碼)原始碼
- 好訊息 OR 壞訊息
- AdWhirl:iPhone,Android軟體內嵌廣告iPhoneAndroid
- 學習Tomcat(七)之Spring內嵌TomcatTomcatSpring
- django的訊號量Django
- RabbitMq中的訊息應答與持久化MQ持久化