Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7791403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:52:32+00:00 2026-06-01T21:52:32+00:00

I use django.contrib.messages module to create flush messages. These messages can be shown in

  • 0

I use django.contrib.messages module to create flush messages. These messages can be shown in different blocks on the page, depends on extra tag:

messages.error(request, 'Error message', extra_tags='form')

I have default tag named ‘global’ and I need to add it in new message if no extra_tags set. So I wrote middleware to wrap django.contrib.messages.add_message() method on application start:

class Initialization:
    def __init__(self):
        self.init_messages()
        raise MiddlewareNotUsed

    def init_messages(self):
        add_message_method = messages.add_message
        def add_message_wrapper(request, level, message, extra_tags='', fail_silently=False):
            if not extra_tags:
                extra_tags = 'global'
            add_message_method(request, level, message, extra_tags, fail_silently)
        messages.add_message = add_message_wrapper

I see that init_messages() method is called, but when I call django.contrib.messages.add_message(), it is not wrapped with add_message_wrapper().

Where is my fail?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:52:33+00:00Added an answer on June 1, 2026 at 9:52 pm

    Couldn’t you just add the “startup code” into settings.py or in views.py instead?

    If all you are doing is adding a default value "global" whenever extra_tags is empty, you could also do that within the page template while rendering your messages:

    {% if not message_obj.tags %}
        // Use "global"
    {% else %}
        // Use {{message_obj.tags}}
    {% endif %}
    

    This can also be done inside your view code:

    storage = messages.get_messages(request)
    for message in storage:
        if not message.tags:
            message.tags = "global"
    storage.used = False
    

    See the Django docs for more info https://docs.djangoproject.com/en/dev/ref/contrib/messages/#expiration-of-messages

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you use contrib.auth.models.User or any of contrib.auth along with Django MongoDB Engine ?
So I simply want to use the delete() from the django.contrib.comments.views.moderation module, but only
I want to use django.contrib.comments in a blogging application and customize the way the
Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
I am working to use django's ContentType framework to create some generic relations for
This is the form file of django.contrib.comments.forms: https://github.com/django/django/blob/master/django/contrib/comments/forms.py I need to create an object
Does anyone know how you can use Django's templatetags in view? I want to
I would like to use the django contrib.admin pages to edit my models, but
My 1.2.3 django site uses contrib.auth and my goal is to use the wkhtmltopdf
I've implemented a notification bar using django.contrib.messages . Now, I want to make extended

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.