Django django.contrib.messages 添加新的常量消息。注意

问题描述 投票:0回答:1

如何为 Django 消息创建新常量?

除了现有的六个常量之外,我想添加一个新的常量

messages.NOTICE
。我可以用它来使用 Bootstrap CSS 显示通知。

# settings.py
from django.contrib.messages import constants as messages

MESSAGE_TAGS = {
        messages.DEBUG: 'alert-secondary',
        messages.INFO: 'alert-info',
        messages.SUCCESS: 'alert-success',
        messages.WARNING: 'alert-warning',
        messages.ERROR: 'alert-danger',
        #messages.NOTICE: 'alert-primary', #To add
}

如果我尝试添加上述内容,则会出错。

    G:\runtime\devui\devui\settings.py changed, reloading.
Traceback (most recent call last):
  File "G:\runtime\devui\manage.py", line 22, in <module>
    main()
  File "G:\runtime\devui\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\devuser\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\devuser\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 382, in execute
    settings.INSTALLED_APPS
  File "C:\Users\devuser\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\conf\__init__.py", line 89, in __getattr__
    self._setup(name)
  File "C:\Users\devuser\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\conf\__init__.py", line 76, in _setup
    self._wrapped = Settings(settings_module)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\devuser\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\conf\__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\devuser\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "G:\runtime\devui\devui\settings.py", line 44, in <module>
    messages.NOTICE: 'alert-primary',
    ^^^^^^^^^^^^^^^
AttributeError: module 'django.contrib.messages.constants' has no attribute 'NOTICE'
PS G:\runtime\devui>
python python-3.x django bootstrap-5 django-messages
1个回答
0
投票

您无法将新关卡添加到

messages
集合中。事实上,如果您需要的只是该级别的存在,那么您不必将其添加到任何地方,您只需将其传递给
add_message
方法 - 但要使消息的 CSS 部分正常工作,您必须添加它到
MESSAGES_TAGS
中的
settings.py

消息级别只是整数。这意味着您可以直接添加整数,或者如果需要,您可以为其创建一个常量,我将在下面展示这两个内容。

# settings.py
from django.contrib.messages import constants as messages

NOTICE: 60 # Or some other integer

MESSAGE_TAGS = {
        messages.DEBUG: 'alert-secondary',
        messages.INFO: 'alert-info',
        messages.SUCCESS: 'alert-success',
        messages.WARNING: 'alert-warning',
        messages.ERROR: 'alert-danger',
        NOTICE: 'alert-primary',
        80: 'alert-tertiary',
}
# elsewhere.py
from django.contrib import messages
messages.add_message(request, 60, "Hello world.") # Would trigger the `NOTICE` CSS class from `MESSAGE_TAGS`

如果您希望统一方便地将常量传递到消息调用,请根据原始常量创建自定义集合(

enum
或Django *
Choice
类):

# constants.py
from django.contrib.messages import constants as message_constants
from django.db import models

class MessageLevels(models.IntegerChoices):
    DEBUG    = message_constants.DEBUG,
    INFO     = message_constants.INFO,
    [...] # repeat for the standard levels

    NOTICE   = 60
    CRITICAL = 90
# elsewhere.py
from ./constants import MessageLevels as msg

messages.add_message(request, msg.NOTICE, "Hello world.")
messages.add_message(request, msg.DEBUG, "Hello debugger.")

如最初所述 - 请确保将消息级别集合中的自定义整数也添加到

MESSAGE_TAGS
,否则不会有任何 CSS 直通。您也可以(并且可能应该)使用该集合:

# settings.py
from ./constants import MessageLevels as msg

MESSAGE_TAGS = {
        messages.DEBUG: 'alert-secondary',
        messages.INFO: 'alert-info',
        messages.SUCCESS: 'alert-success',
        messages.WARNING: 'alert-warning',
        messages.ERROR: 'alert-danger',
        msg.NOTICE, 'alert-primary',
        msg.CRITICAL, 'alert-tertiary',
}
© www.soinside.com 2019 - 2024. All rights reserved.