How can I send Message.info from admin to another user in Django

When someone user tries to publish an article. The user must wait for the approval/disapproval of this article from the admin.

after approving/disapproving their articles, the users must get Message.info. But these messages showed on the admin page, not on a user page

the disapproved message:

this is model.py


class Article(models.Model):
    auther = models.ForeignKey(User, on_delete= models.CASCADE)
    title = models.CharField(max_length= 200, null = False)
    bio = models.CharField(max_length = 450, null = False)
    tags = TaggableManager()
    body = RichTextField()
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)
    approved = models.BooleanField('approved', default = 'False')

    
    def __str__(self):
        return self.title


this viws.py :


from django.contrib.auth.models import User
from article.models import Article


def delete_article(request, pk):  
    model_article = apps.get_model('article','Article')
    if request.user.is_superuser:
        id_user = model_article.objects.get(pk = pk).auther_id

        model_article.objects.get(pk = pk).delete()
        messages.info(request,'  تم تنفيذ العملية بنجاح!' )
    for x in User.objects.all():
        if(x.id == id_user):
            messages.info(request,
        'تم حذف مقالك الجديد لأنه يخالف سياسة الموقع !')
            return redirect('main')

But i want the message shwon to user who want to publish an article

To show the message to the user who wants to publish an article, you can use the Django message framework. This framework allows you to set messages that will be displayed to the user on the next page load.

To use the message framework, you will need to include the messages middleware in your Django settings file:

MIDDLEWARE = [
    # ...
    'django.contrib.messages.middleware.MessageMiddleware',
    # ...
]

You will also need to include the messages context processor in your Django settings file:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # ...
                'django.contrib.messages.context_processors.messages',
                # ...
            ],
        },
    },
]

Once you have set up the message framework, you can use the messages.add_message function to set a message that will be displayed to the user on the next page load.

Example:

from django.contrib import messages

def delete_article(request, pk):  
    model_article = apps.get_model('article','Article')
    if request.user.is_superuser:
        id_user = model_article.objects.get(pk = pk).auther_id

        model_article.objects.get(pk = pk).delete()
        messages.add_message(request, messages.INFO, 'تم تنفيذ العملية بنجاح!')
    for x in User.objects.all():
        if(x.id == id_user):
            messages.add_message(request, messages.INFO, 'تم حذف مقالك الجديد لأنه يخالف سياسة الموقع!')
            return redirect('main')

To display the messages to the user, you will need to include the messages template tag in your template and use the {% if messages %} template tag to check if there are any messages to display.

E.g.:

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

This will display a list of messages to the user. You can customize the appearance of the messages by using CSS styles to format the ul.messages and li.messages elements.

To display the second message ('تم حذف مقالك الجديد لأنه يخالف سياسة الموقع !') to the user who is trying to publish the article, you can do the following:

In your delete_article view function, after you delete the article and send the Message.info to the request, you can get the User object of the user who created the article using the auther_id field:

 user = User.objects.get(id=id_user)

You can then use Django's built-in messaging system to send a message to the user. To do this, you need to import the messages module and use the add_message function.

from django.contrib import messages

#send the message to the user
messages.add_message(request, messages.INFO, 'تم حذف مقالك الجديد لأنه يخالف     سياسة الموقع !')

To display the message to the user, you need to include the messages template tag in your template and use the {% if messages %} template tag to check if there are any messages to display:

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

You can then redirect the user back to the page where they can view the message.

Back to Top