Login

Adding Object name in Success Message

Author:
Pr0digal
Posted:
February 5, 2019
Language:
Python
Version:
2.0
Tags:
success_message
Score:
0 (after 0 ratings)

Success Message has object properties

Success Message can have an object name or the properties of the object model to collide with the message see code below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django.views.generic import UpdateView
from django.contrib.messages.views import SuccessMessageMixin
from django.contrib.auth.mixins import LoginRequiredMixin

class AccountsUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):

    template_name = 'accounts/user.html' 
    form_class = auth_login.CustomUserChangeForm # your own forms
    success_url = reverse_lazy('accounts:account_update_view')
    success_message = _("%(first_name)s %(last_name)s was updated successfully")
    
     # Get the object name attribute and properties in your own model
    def get_success_message(self, cleaned_data):
        return self.success_message % dict(
            cleaned_data,
            first_name=self.object.first_name,
            last_name=self.object.last_name
        )

More like this

Comments

Please login first before commenting.