how to send success message if we use django generic views how to send success message if we use django generic views python python

how to send success message if we use django generic views


As of Django 1.6+, using any class-based generic views, you can rely on the successMessageMixin. It's as simple as adding the mixin to your class definition and setting success_message attribute to whatever you want.

As Olivier Verdier mentioned, please remember to display messages in your main template!

a simple example from the docs:

from django.contrib.messages.views import SuccessMessageMixinfrom django.views.generic.edit import CreateViewfrom myapp.models import Authorclass AuthorCreate(SuccessMessageMixin, CreateView):    model = Author    success_url = '/success/'    success_message = "%(name)s was created successfully"

a more complex example:

from django.contrib.messages.views import SuccessMessageMixinfrom django.views.generic.edit import CreateViewfrom myapp.models import ComplicatedModelclass ComplicatedCreate(SuccessMessageMixin, CreateView):    model = ComplicatedModel    success_url = '/success/'    success_message = "%(calculated_field)s was created successfully"    def get_success_message(self, cleaned_data):        #  cleaned_data is the cleaned data from the form which is used for string formatting        return self.success_message % dict(cleaned_data,                                           calculated_field=self.object.calculated_field)


As far as I know, there isn't a straightforward way of doing this using traditional generic views. I've always felt that the documentation on generic views was pretty lacking and so never used them.

In theory you could use a decorator by making the assumption that a redirect meant a successful submission.

So you could write something like this (none of this code has been tested):

urls.py:

try:    from functools import wrapsexcept ImportError:    from django.utils.functional import wrapsfrom django.http import HttpRedirectResponsefrom django.contrib import messagesfrom django.views.generic import * def add_message(success_message=None):    def decorator(func):        def inner(request, *args, **kwargs):            resp = func(request, *args, **kwargs)            if isinstance(resp, HttpRedirectResponse):                messages.success(request, message)            return resp        return wraps(func)(inner)    return decoratorstudent_info_edit = {  'template_name': 'myapp/student/form.html',  'template_object_name': 'student',  'form_class':  studentForm,}student_info_new = {  'template_name': 'myapp/student/form.html',  'form_class':  studentForm,  'post_save_redirect': '/myapp/students/',}urlpatterns += patterns('',  url(r'^students/$', list_detail.object_list, { 'queryset': Student.objects.all() }, name="students"),  url(r'^students/(?P<object_id>\d+)/$', add_message("Student record updated successfully")(create_update.update_object), student_info_edit, name="student_detail"),  url(r'^students/new$', add_message("The student was added successfully.")(create_update.create_object), student_info_new, name="student_new"),)

All that said and coded, Django 1.3 introduced class-based generic views, so if you're interested in moving onto Django 1.3 you should look into those. They may allow more customization, not sure.

In the long run I rarely see the benefit form using generic views, and this goes double for things like add/update.


The functionality that you are asking for is already implemented in Django generic views:

https://github.com/django/django/blob/1.2.X/django/views/generic/create_update.py#L115

You will see the messages by displaying messages in your main template.