I'm trying to write a CRUD application using Djangos class based generic views. Following is the code i wrote to create a new user in the db.
from django.views.generic import CreateView
from django.contrib.auth.decorators import login_required
from django.contrib import messages
class UserCreateView(CreateView):
"""
Display and accept a new user to be created in db
"""
form_class = ProfileForm
template_name = 'userdb/profile_form.html'
success_url = '/organization/users/'
def post(self, request, *args, **kwargs):
messages.success(request, "Success", extra_tags='msg')
return super(UserCreateView, self).post(request, *args, **kwargs)
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(UserCreateView, self).dispatch(*args, **kwargs)
Note that to add a success message to be displayed to the user I've had to extend the post function. I know this is not a good way to do this as, when this function gets called it's not decided whether the submitted form contains valid data. So my question is, Is there recommended way of combining Djangos messaging framework with class based generic views?