Login

Adding Object name in Success Message

Author:
Pr0digal
Posted:
February 5, 2019
Language:
Python
Version:
2.0
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

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

Please login first before commenting.