[Fixed]-Problem with class based generic views in Django


The answer depends on what specifically you’re looking to do with the messaging framework. If it needs to be called for every get request you’d naturally need to put it in the get method (point being there’s no one right place to put this code).

Anyways, it sounds like you’re looking for a place that’s only triggered when the form is valid.

CreateView uses the ModelFormMixin which implements a form_valid method which is only fired upon successful form saving. Perfect!

def form_valid(self, form):
    messages.success(self.request, "Success", extra_tags='msg')
    return super(UserCreateView, self).form_valid(form)  
    # ModelFormMixin will now save
    # FormMixin will now redirect to success_url()
    # override above behavior if you need to do something with the object

Leave a comment