Django管理操作中的自定义错误消息

时间:2022-01-18 23:19:56

I've written custom admin actions that basically do QuerySet.update() for certain fields in the model. There are times when these actions shouldn't be allowed to complete -- instead, they should display an error and not do anything. I've tried message_user, but that displays a green checkmark, whereas I'd like it to display the Django admin error message.

我已经编写了自定义管理操作,这些操作基本上是为模型中的某些字段执行query .update()。有些时候,这些操作不应该被允许完成——相反,它们应该显示一个错误而不做任何事情。我尝试过message_user,但它显示了一个绿色的复选标记,而我希望它显示Django管理错误消息。

A solution I've found online is to use a ModelForm, but I don't think that applies in this case, as here everything happens on the admin change list page.

我在网上找到的一个解决方案是使用ModelForm,但我认为在这种情况下不适用,因为这里的一切都发生在admin change list页面上。

4 个解决方案

#1


27  

The message_user function used within the admin simply uses the contrib.messages package. You could try something like this:

管理员中使用的message_user函数只使用这个方法。消息包。你可以试试这样的方法:

from django.contrib import messages

# Then, when you need to error the user:
messages.error(request, "The message")

You can also use warning, debug, info and success in place of error

您还可以使用警告、调试、信息和成功来代替错误

Hope that helps!

希望会有帮助!

#2


26  

from django.contrib import messages
...
self.message_user(request, "The message", level=messages.ERROR)

Сan also be used (messages.ERROR, messages.WARNING, messages.DEBUG, messages.INFO, messages.SUCCESS)

Сan也被使用(消息。错误消息。警告消息。调试消息。信息,messages.SUCCESS)

#3


2  

Not sure whether this was fixed in newer django versions (I found the behaviour you described in django 1.2.1 and also in django-grappelli 2.0), but if you use Bartek's method above, you'd also probably want to change the admin templates to show the messages differently. Specifically in base.html:

不确定这是否在更新的django版本中得到了修复(我在django 1.2.1和django-grappelli 2.0中发现了您所描述的行为),但是如果您使用上面的Bartek方法,您可能还想更改管理模板,以显示不同的消息。特别是在base.html:

Change this:

改变:

{% if messages %}
        <ul class="messagelist">{% for message in messages %}<li>{{ message }}</li>{% endfor %}</ul>
    {% endif %}

to this:

:

{% if messages %}
        <ul class="messagelist">{% for message in messages %}<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message}}</li>{% endfor %}</ul>
    {% endif %}

You might still need to tweak some CSS on top of that too, but at least it would come up as a different li class on the HTML.

您可能还需要在此基础上修改一些CSS,但至少它将作为HTML上的另一个li类出现。

Here's a sample CSS change (compatible with grappelli)

下面是一个CSS更改示例(与grappelli兼容)

ul.messagelist li.error {
background: url('../img/icons/icon-no.png') 20px 50% no-repeat;
background-color: #f2e6e6;

}

#4


1  

You can use django.contrib.messages backend

您可以使用django.contrib。消息后端

def my_action(self, request, queryset):
  #do something
  from django.contrib import messages
  messages.error(request,'Error message')

This will show the error message and the red error sign.

这将显示错误消息和红色错误标志。

#1


27  

The message_user function used within the admin simply uses the contrib.messages package. You could try something like this:

管理员中使用的message_user函数只使用这个方法。消息包。你可以试试这样的方法:

from django.contrib import messages

# Then, when you need to error the user:
messages.error(request, "The message")

You can also use warning, debug, info and success in place of error

您还可以使用警告、调试、信息和成功来代替错误

Hope that helps!

希望会有帮助!

#2


26  

from django.contrib import messages
...
self.message_user(request, "The message", level=messages.ERROR)

Сan also be used (messages.ERROR, messages.WARNING, messages.DEBUG, messages.INFO, messages.SUCCESS)

Сan也被使用(消息。错误消息。警告消息。调试消息。信息,messages.SUCCESS)

#3


2  

Not sure whether this was fixed in newer django versions (I found the behaviour you described in django 1.2.1 and also in django-grappelli 2.0), but if you use Bartek's method above, you'd also probably want to change the admin templates to show the messages differently. Specifically in base.html:

不确定这是否在更新的django版本中得到了修复(我在django 1.2.1和django-grappelli 2.0中发现了您所描述的行为),但是如果您使用上面的Bartek方法,您可能还想更改管理模板,以显示不同的消息。特别是在base.html:

Change this:

改变:

{% if messages %}
        <ul class="messagelist">{% for message in messages %}<li>{{ message }}</li>{% endfor %}</ul>
    {% endif %}

to this:

:

{% if messages %}
        <ul class="messagelist">{% for message in messages %}<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message}}</li>{% endfor %}</ul>
    {% endif %}

You might still need to tweak some CSS on top of that too, but at least it would come up as a different li class on the HTML.

您可能还需要在此基础上修改一些CSS,但至少它将作为HTML上的另一个li类出现。

Here's a sample CSS change (compatible with grappelli)

下面是一个CSS更改示例(与grappelli兼容)

ul.messagelist li.error {
background: url('../img/icons/icon-no.png') 20px 50% no-repeat;
background-color: #f2e6e6;

}

#4


1  

You can use django.contrib.messages backend

您可以使用django.contrib。消息后端

def my_action(self, request, queryset):
  #do something
  from django.contrib import messages
  messages.error(request,'Error message')

This will show the error message and the red error sign.

这将显示错误消息和红色错误标志。