我正在关注此特定教程(tutorial link)以使我的动态表单集正常工作。我遇到了这个错误,我不确定它源于何处或如何解决它。有人能告诉我可以做些什么来解决它吗?是的,我知道这是一个重复的问题。我已经完成了很多这些答案,但在我的案例中无法弄清楚。
查看代码:
class ProfileList(ListView):
model = Profile
class ProfileFamilyMemberCreate(CreateView):
model = Profile
fields = ['first_name', 'last_name']
success_url = reverse_lazy('purchase_order')
def get_context_data(self, **kwargs):
data = super(ProfileFamilyMemberCreate, self).get_context_data(**kwargs)
if self.request.POST:
data['familymembers'] = FamilyMemberFormSet(self.request.POST)
else:
data['familymembers'] = FamilyMemberFormSet()
return data
def form_valid(self, form):
context = self.get_context_data()
familymembers = context['familymembers']
with transaction.atomic():
self.object = form.save()
if familymembers.is_valid():
familymembers.instance = self.object
familymembers.save()
return super(ProfileFamilyMemberCreate, self).form_valid(form)
Forms.py
class FamilyMemberForm(forms.ModelForm):
class Meta:
model = FamilyMember
exclude = ()
FamilyMemberFormSet = inlineformset_factory(Profile, FamilyMember,
form=FamilyMemberForm, extra=1)
回溯:
回溯:
File "/Users/thahaabdulwahid/miniconda3/envs/projectEnv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/Users/thahaabdulwahid/miniconda3/envs/projectEnv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/Users/thahaabdulwahid/miniconda3/envs/projectEnv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /documents/purchase_order/
Exception Value: __init__() takes 1 positional argument but 2 were given
答案 0 :(得分:1)
您的问题与formset无关,但是在url配置中。对于基于类的视图,您需要在模式中调用其as_view()
方法:
url(r'^purchase_order/$', views.ProfileList.as_view(), name='purchase_order')