Opened 13 months ago

Closed 13 months ago

Last modified 13 months ago

#34923 closed Bug (fixed)

MESSAGE_TAGS ignored in 5.0a1

Reported by: James Gillard Owned by: Mariusz Felisiak
Component: contrib.messages Version: 5.0
Severity: Release blocker Keywords:
Cc: François Freitag Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've been running 5.0a1 off and on with our project to spot issues and have come across my first.

As stated in the title, MESSAGE_TAGS in settings.py is now having no effect for me. I noticed this when the extra classes we define inside settings.py disappeared from the rendered HTML of a message. I tried having a play around with the django test suite to recreate, but I didn't achieve that, so the proof I have is from adding a debug line inside django.contrib.messages.utils.get_level_tags. I stopped at this point, as someone might be able to point out something obvious I've missed that's leading to this.

Here's the debug print I've added:

def get_level_tags():
    """
    Return the message level tags.
    """
    print(getattr(settings, "MESSAGE_TAGS", {}))
    return {
        **constants.DEFAULT_TAGS,
        **getattr(settings, "MESSAGE_TAGS", {}),
    }

Here is what we have inside settings.py:

MESSAGE_TAGS = {
    messages.DEBUG: 'debug alert-info is-info',
    messages.INFO: 'info alert-info is-info',
    messages.SUCCESS: 'success alert-success is-success',
    messages.WARNING: 'warning alert-warning is-warning',
    messages.ERROR: 'error alert-danger is-danger',
}

And here's what I'm seeing when going from 4.2.5 to 5.0a1, and also testing latest main; lines removed for brevity:

✗ pip install Django==4.2.5
✗ ./manage.py runserver
{10: 'debug alert-info is-info', 20: 'info alert-info is-info', 25: 'success alert-success is-success', 30: 'warning alert-warning is-warning', 40: 'error alert-danger is-danger'}
{10: 'debug alert-info is-info', 20: 'info alert-info is-info', 25: 'success alert-success is-success', 30: 'warning alert-warning is-warning', 40: 'error alert-danger is-danger'}
Django version 4.2.5, using settings 'journee.settings'
                                                                                                                                                                                                                                                     
✗ pip install Django==5.0a1
✗ ./manage.py runserver
{}
{}
Django version 5.0a1, using settings 'journee.settings'

✗ pip install git+https://github.com/django/django.git
✗ ./manage.py runserver
{}
{}
Django version 5.1.dev20231023125957, using settings 'journee.settings'

Change History (9)

comment:1 by James Gillard, 13 months ago

Component: Uncategorizedcontrib.messages

comment:2 by Mariusz Felisiak, 13 months ago

Cc: François Freitag added
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Thanks for the report! Regression in b7fe36ad37fb18c4bc7932c0aec6ae4f299b9622.

comment:3 by Mariusz Felisiak, 13 months ago

James, Can you confirm that the following patch fixes this issue for you?

  • django/contrib/messages/storage/base.py

    diff --git a/django/contrib/messages/storage/base.py b/django/contrib/messages/storage/base.py
    index 124049a5e2..5d89acfe69 100644
    a b  
    11from django.conf import settings
    22from django.contrib.messages import constants, utils
     3from django.utils.functional import SimpleLazyObject
    34
    4 LEVEL_TAGS = utils.get_level_tags()
     5LEVEL_TAGS = SimpleLazyObject(utils.get_level_tags)
    56
    67
    78class Message:

in reply to:  3 comment:4 by James Gillard, 13 months ago

Hi Mariusz, that does indeed fix it for me!

comment:5 by Mariusz Felisiak, 13 months ago

Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:6 by Mariusz Felisiak, 13 months ago

Has patch: set

comment:7 by Natalia Bidart, 13 months ago

Triage Stage: AcceptedReady for checkin

comment:8 by Natalia <124304+nessita@…>, 13 months ago

Resolution: fixed
Status: assignedclosed

In 311718fe:

Fixed #34923 -- Avoided initializing LEVEL_TAGS when importing django.contrib.messages.

Regression in b7fe36ad37fb18c4bc7932c0aec6ae4f299b9622.

Thanks James Gillard for the report.

comment:9 by Natalia <124304+nessita@…>, 13 months ago

In 00805ff:

[5.0.x] Fixed #34923 -- Avoided initializing LEVEL_TAGS when importing django.contrib.messages.

Regression in b7fe36ad37fb18c4bc7932c0aec6ae4f299b9622.

Thanks James Gillard for the report.

Backport of 311718feb5f1fb9ff794bbac0cda48cfc3410de8 from main

Note: See TracTickets for help on using tickets.
Back to Top