[Answered ]-Django update view and passing context

2đź‘Ť

âś…

You shouldn’t be overriding post at all. All of that logic should happen in get_context_data.

In fact, none of your overrides are needed. Everything that you do in form_valid will be done already by the standard form save. And overriding dispatch just to call the superclass is pointless.

Your view should look like this only, with no overridden methods at all:

class GeneralUserUpdateView(UpdateView):
    model = GeneralUser
    form_class = GeneralUserChangeForm
    template_name = "general_user_change.html"
    context_object_name = 'picture'

(although it seems a little odd that you want to refer to an instance of GeneralUser as “picture”).

Edit to redirect to a specific URL, you can define get_success_url:

    def get_success_url(self):
        return reverse("user_profile", self.kwargs['pk'], self.kwargs['username'])

Leave a comment