谢谢你们的回复。尽管我将代码更改为以下内容,但我仍然遇到相同的异常:
class ProjectUpdateView(UpdateView):
form_class = ProjectForm
template_name = 'projects/project_create.html'
success_url = reverse_lazy('projects-list')
def get_object(self, queryset=None):
obj = Project.objects.get(id=self.kwargs['pk'])
return obj
def get_context_data(self, **kwargs):
context = super(ProjectUpdateView, self).get_context_data(**kwargs)
project = Project.objects.get(id=self.kwargs['pk'])
if self.request.POST:
context['reward_formset'] = ProjectRewardFormSet(self.request.POST, self.request.FILES, instance=project)
else:
context['reward_formset'] = ProjectRewardFormSet(instance=project)
return context
def form_valid(self, form):
context = self.get_context_data()
reward_formset = context['reward_formset']
if reward_formset.is_valid():
self.object = form.save(commit=False)
self.object.owner = self.request.user
self.object.save()
reward_formset.instance = self.object
reward_formset.save()
return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response(self.get_context_data(form=form))
以下是回溯:
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/contrib/auth/decorators.py in _wrapped_view
20. return view_func(request, *args, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/views/generic/base.py in view
48. return self.dispatch(request, *args, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/views/generic/base.py in dispatch
69. return handler(request, *args, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/views/generic/edit.py in post
172. return super(BaseCreateView, self).post(request, *args, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/views/generic/edit.py in post
138. return self.form_valid(form)
...
▶ Local vars
/home/lara/.venv/reztarter/reztarter/../reztarter/projects/views.py in form_valid
22. context = self.get_context_data()
...
▶ Local vars
/home/lara/.venv/reztarter/reztarter/../reztarter/projects/views.py in get_context_data
39. context['reward_formset'] = ProjectRewardFormSet(self.request.POST)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/models.py in __init__
697. queryset=qs, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/models.py in __init__
424. super(BaseModelFormSet, self).__init__(**defaults)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/formsets.py in __init__
50. self._construct_forms()
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/formsets.py in _construct_forms
115. self.forms.append(self._construct_form(i))
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/models.py in _construct_form
706. form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/forms/models.py in _construct_form
451. kwargs['instance'] = self.get_queryset()[i]
...
▶ Local vars
/home/lara/.venv/reztarter/lib/python2.7/site-packages/django/db/models/query.py in __getitem__
190. return self._result_cache[k]
...
▶ Local vars