开发者

Why might django messages not have tags?

开发者 https://www.devze.com 2023-03-17 18:10 出处:网络
I\'m trying to disable the add/change for certa开发者_Go百科in models, and I\'m doing so by overriding the add_view method in the admin object. In so doing, I want to add a message to the user, then r

I'm trying to disable the add/change for certa开发者_Go百科in models, and I'm doing so by overriding the add_view method in the admin object. In so doing, I want to add a message to the user, then redirect. I'm adding the message this way:

    messages.add_message(
        request,
        messages.ERROR,
        "Please use the other interface.",
        )

after following this:

http://readthedocs.org/docs/django/en/latest/ref/contrib/messages.html

And it works, but it shows up with a little green check mark next to it, as if it were a success. I checked the template, and it adds whatever classes are in message.tags (and if there's warning, or error, you get different icons than the check mark). But when I make a message the way that link above says (or any other way I've found), I don't see tags on the message object.

Any ideas what might be going wrong?


I just had an experience yesterday where I encountered the exact same thing. What solved it for me, believe it or not, was restarting the web service. I was working locally with ./manage.py runserver so not sure whether that was your scenario or not.

Incidentally, I have found other bizarre django oddities, particularly when creating a lot of new code, solved by doing this.


Make sure that in settings.py, the MIDDLEWARE_CLASSES list contains

    'django.contrib.messages.middleware.MessageMiddleware',

Otherwise, Django messages sort of work, but only for authenticated users, with broken message tags, and possibly other problems.

(I just ran into the same problem, and adding that line fixed it.)


This is what is in my django admin base.css:

ul.messagelist li {
    font-size: 12px;
    display: block;
    padding: 4px 5px 4px 25px;
    margin: 0 0 3px 0;
    border-bottom: 1px solid #ddd;
    color: #666;
    background: #ffc url(../img/admin/icon_success.gif) 5px .3em no-repeat;
}

Which means despite it's class or tags, it gets the icon. Just has to be a message <li> in the messagelist.

0

精彩评论

暂无评论...
验证码 换一张
取 消