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 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.