django - 带有断线和重定向 url 的 Django 消息
问题描述
我想改进我django message
的一些事情:breakline
在文本中添加一个并使用django reverse url
.
这是我的信息:
messages.error(self.request, _( f"The link to download the document has expired. Please request it again in our catalogue : {redirect to freepub-home}"
我想通过在之后添加一个新行来分隔我的消息,.
以便获得如下内容:
messages.error(self.request, _( f"The link to download the document has expired.
Please request it again in our catalogue : {redirect to freepub-home}"
那么,由于反向 url to ,我如何在我的消息中设置 django 重定向"freepub-home"
?
提前谢谢你!
编辑 :
我克服了设置断线:
messages.error(self.request, mark_safe(
"The link to download the document has expired."
"<br />"
"Please request it again in our catalogue :
<a href='{% url "freepub-home" %}'> my link </a>")
但是到目前为止我还没有找到如何在里面传递 django url,因为我有引号和双引号的问题。
解决方案
您传递给mark_safe
的是一个纯字符串,它不会被解释为 Django 模板,因此您不能在其中使用模板标签语法。您必须使用该reverse()
函数来获取 url 和 python 字符串格式化语法来构建消息:
from django.core.urlresolvers import reverse
# ...
# using triple-quoted string makes life easier
msg = """
The link to download the document has expired.
<br />
Please request it again in our catalogue :
<a href='{url}'> my link </a>
"""
url = reverse("freepub-home")
messages.error(self.request, mark_safe(msg.format(url=url)))
推荐阅读
- javascript - 转换后 React Router 滚动页面到顶部
- google-cloud-platform - 带有身份验证 yaml 配置文件部署错误的 Google 云 API 网关
- ssl - 如何从 *.cer 文件中获取 *.crt 和 *.key?
- javascript - 等待 Promises 完成,然后再将它们推送到数组中(Javascript Promises)
- javascript - 无论滚动如何,如何将位置绝对 div 转换到另一个顶部
- android - Unity Firebase 未在构建上初始化
- c# - 向 JobInputHttp 提交编码的 URL 时,为什么 Azure 媒体服务 v3 作业会失败?
- mysql - 使用 MIN() 和 GROUP BY 时,SQL SELECT 的行为不符合预期
- c++11 - 自动初始化父类和子类中的成员变量
- tensorflow - 错误 pip 从 github 存储库安装 wheel 文件(下载 pycocotools)