[Fixed]-Django Form context data lost on validation fail

0👍

Finally solved it by amending the else clause as follows:

user_object = self.request.user
files = [user_object.logo]
return render_to_response('directory/business_edit.html', {'form': form, 'uploaded_files': files}, context_instance=RequestContext(request))

As @Alasdair pointed out, it seems it is not a good idea to use class based views if you need any sort of custom behaviour. Probably best to use functions/methods from the outset.

1👍

You have not included the context from get_context_data when you call render_to_response, therefore it is not included.

Overriding the post method of UpdateView isn’t a great idea, because you have to duplicate all of the work that the base implementation does for post requests.

Try and override more specific things e.g. form_valid and success_url, instead of overriding the post method.

class BusinessEditView(UpdateView):
    template_name = 'directory/business_edit.html'
    model = Appuser
    form_class = BusinessEditForm
    success_url = '/profile/' # ideally use reverse_lazy here

It is not clear what you want to do when the form is valid — currently you are saving with commit=False, which means that the changes won’t be saved the db — so I can’t offer any advice about how to override form_valid.

Leave a comment