在管理员操作消息中添加新行

时间:2018-11-05 09:12:00

标签: django django-admin

例如,在触发管理员操作之后,如何添加新行。

from django.contrib import messages

messages.error(request, 'This is a line \n and this is another line')

我也尝试了html标记,但是没有用。

2 个答案:

答案 0 :(得分:2)

我认为您需要覆盖admin.base模板的一部分

为了遵守换行符。有内置的模板标签linebreaksbr

base_site.html中的覆盖块为:

<!-- The path and name for this file should be "templates/admin/base_site.html". -->
<!-- It overrides the original admin template and will be picked up by all of its children. -->
<!-- This template override has been verified for Django 1.11 and 2.0. -->

{% extends "admin/base_site.html" %}

{% block messages %}
    {% if messages %}
    <ul class="messagelist">{% for message in messages %}
      {# added linebreaksbr here #}
      <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst|linebreaksbr }}</li>
    {% endfor %}</ul>
    {% endif %}
{% endblock messages %}

示例取自here

答案 1 :(得分:0)

from django.contrib import messages
from django.utils.safestring import mark_safe

messages.error(request, mark_safe('This is a line <br> and this is another line'))