Messages passed to success url of django Update View

时间:2015-07-31 20:21:31

标签: django class django-views messages

Im trying to pass a success message on update using the generic CBV UpdateView..

class TribeUpdate(FormMessagesMixin, UpdateView):
    model = Tribe
    fields = ['name', 'description', 'cause']
    template_name_suffix = '_update_form'
    form_valid_message = 'success'

    def get_success_url(self):
        return reverse('tribe_detail', kwargs={'tribe_slug' : self.kwargs['slug']} )

    def get_object(self, *args, **kwargs):
        obj = super(TribeUpdate, self).get_object(*args, **kwargs)
        if not obj.creator == self.request.user:
            raise PermissionDenied
        return obj

    def form_valid(self, form):
        tribe = Tribe.objects.get(slug=self.kwargs['slug'])
        action.send(tribe.creator, verb='updated the tribe', target=tribe)
        form.save()
        return super(TribeUpdate, self).form_valid(form)

The success message doesnt apear on the success url page.. the tribe_detail view.. it just appears again when i click through to the UpdateView..?

Any help will be amazing, I need to override form_valid to save data to the form depending on the input..

1 个答案:

答案 0 :(得分:0)

At the end of your form_valid method you call itself again, so it goes in recursion.

You just need to overwrite

return super(TribeUpdate, self).form_valid(form)

to

return HttpResponseRedirect(self.get_success_url())

And it will work.