python - Django Form context data lost on validation fail -


my form (code below) loads , saves fine. however, if there validation error in form , reloaded, context['uploaded_files'] assign in get_context_data method empty. why , how can once again pass context['uploaded_files'] when form reloaded on validation fail?

class businesseditview(updateview):     template_name = 'directory/business_edit.html'     model = appuser     form_class = businesseditform      def get_context_data(self, **kwargs):         context = super(businesseditview, self).get_context_data(**kwargs)         user_object = context['appuser']         files = [user_object.logo]         context['uploaded_files'] = files         return context      def get_object(self, queryset=none):         return self.request.user      def post(self, request, *args, **kwargs):         form = businesseditform(request.post, instance=self.request.user)         if form.is_valid():             form.save(commit=false)             return httpresponseredirect('/profile/')         else:             return render_to_response('directory/business_edit.html', {'form': form}, context_instance=requestcontext(request)) 

as per @alasdair's suggestion, amended else clause follows:

context = self.get_context_data() return render_to_response('directory/business_edit.html', context, context_instance=requestcontext(request)) 

it however, caused exception follows:

traceback: file "/users/h/.virtualenvs/pony/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response   111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs) file "/users/h/.virtualenvs/pony/lib/python2.7/site-packages/django/views/generic/base.py" in view   69.             return self.dispatch(request, *args, **kwargs) file "/users/h/.virtualenvs/pony/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch   87.         return handler(request, *args, **kwargs) file "/users/h/development/pony/pony/directory/views.py" in post   296.             context = self.get_context_data() file "/users/h/development/pony/pony/directory/views.py" in get_context_data   272.         context = super(businesseditview, self).get_context_data(**kwargs) file "/users/h/.virtualenvs/pony/lib/python2.7/site-packages/django/views/generic/detail.py" in get_context_data   100.         if self.object:  exception type: attributeerror @ /profile/business/edit/ exception value: 'businesseditview' object has no attribute 'object' 

you have not included context get_context_data when call render_to_response, therefore not included.

overriding post method of updateview isn't great idea, because have duplicate of work base implementation post requests.

try , override more specific things e.g. form_valid , success_url, instead of overriding 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 not clear want when form valid -- saving commit=false, means changes won't saved db -- can't offer advice how override form_valid.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -