My form (code below) loads and saves fine. However, if there is a validation error in the form and it is reloaded, the context['uploaded_files'] which I assign in the get_context_data method is empty. Why is that and how can I once again pass the context['uploaded_files'] it when the form is 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, I amended the else clause as follows:
context = self.get_context_data()
return render_to_response('directory/business_edit.html', context, context_instance=RequestContext(request))
It however, caused an exception as 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 at /profile/business/edit/
Exception Value: 'BusinessEditView' object has no attribute 'object'
Aucun commentaire:
Enregistrer un commentaire