验证时丢失的Django表单上下文数据失败

时间:2015-06-25 15:54:31

标签: python django django-forms

我的表单(下面的代码)加载并保存正常。但是,如果表单中存在验证错误并重新加载,则我在get_context_data方法中指定的context['uploaded_files']为空。为什么这样,当验证失败时重新加载表单时,如何再次传递context['uploaded_files']

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))

根据@ Alasdair的建议,我将else条款修改如下:

context = self.get_context_data()
return render_to_response('directory/business_edit.html', context, context_instance=RequestContext(request))
然而,它引起了如下例外:

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'

2 个答案:

答案 0 :(得分:1)

当您致电get_context_data时,您尚未加入render_to_response的背景信息,因此不会包含该信息。

覆盖post UpdateView方法并不是一个好主意,因为您必须复制基本实现对帖子请求所做的所有工作。

尝试并覆盖更具体的内容,例如form_validsuccess_url,而不是覆盖post方法。

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

当表单有效时,您不清楚自己想要做什么 - 目前您使用commit=False进行保存,这意味着更改不会保存在数据库中 - 所以我无法提供关于如何覆盖form_valid的任何建议。

答案 1 :(得分:0)

最后通过修改else子句解决了这个问题:

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))

正如@Alasdair指出的那样,如果您需要任何类型的自定义行为,那么使用基于类的视图似乎不是一个好主意。可能最好从一开始就使用函数/方法。