我使用相同的crispy形式进行添加和编辑,并添加了一个变量,因此我可以将提交按钮文本从添加更改为编辑,反之亦然。
但是,添加视图会出现以下错误:
Traceback: (removed the in built references)
...
File "/itapp/itapp/sites/views.py" in dispatch
954. return super(AddSubnet, self).dispatch(*args, **kwargs)
...
File "/itapp/itapp/sites/views.py" in get_context_data
969. context = super().get_context_data(**kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/generic/edit.py" in get_context_data
93. kwargs['form'] = self.get_form()
File "/usr/local/lib/python3.6/site-packages/django/views/generic/edit.py" in get_form
45. return form_class(**self.get_form_kwargs())
Exception Type: TypeError at /sites/site/add_subnet/7
Exception Value: 'SubnetForm' object is not callable
我不确定为什么因为表格的代码看起来对我不熟练的眼睛来说至少无论如何
forms.py:
class SubnetForm(forms.ModelForm):
class Meta:
model = SiteSubnets
fields = ['subnet', 'subnet_type', 'circuit', 'device_data', 'vlan_id', 'peer_desc']
def __init__(self, *args, **kwargs):
site_id = kwargs.pop('site_id', None)
self.is_add = kwargs.pop("is_add", False)
super(SubnetForm, self).__init__(*args, **kwargs)
self.fields['circuit'].queryset = Circuits.objects.filter(site_data=site_id)
self.helper = FormHelper(self)
self.helper.form_id = 'subnet_form'
self.helper.form_method = 'POST'
self.helper.add_input(Submit('submit', 'Add Subnet' if self.is_add else 'Edit Subnet', css_class='btn-primary'))
self.helper.layout = Layout(
Div(
Div(
Field('subnet', placeholder='Subnet'),
Div('subnet_type', title="Subnet Type"),
css_class='col-lg-3'
),
Div(
Div('circuit', title='Circuit'),
Div('device_data', title="Device Data"),
css_class='col-lg-3'
),
Div(
Field('vlan_id', placeholder='VLAN ID'),
Field('peer_desc', placeholder="Peer Description"),
css_class='col-lg-3'
),
css_class='row'
)
)
查看:
class AddSubnet(CreateView):
form_class = SubnetForm(is_add=True)
template_name = "sites/subnet_form.html"
@method_decorator(user_passes_test(lambda u: u.has_perm('config.add_subnet')))
def dispatch(self, *args, **kwargs):
self.site_id = self.kwargs['site_id']
self.site = get_object_or_404(SiteData, pk=self.site_id)
return super(AddSubnet, self).dispatch(*args, **kwargs)
def get_success_url(self, **kwargs):
return reverse_lazy("sites:site_detail_subnets", args=(self.site_id,))
def form_valid(self, form):
form.instance.site_data = self.site
return super(AddSubnet, self).form_valid(form)
def get_form_kwargs(self, *args, **kwargs):
kwargs = super().get_form_kwargs()
kwargs['site_id'] = self.site_id
return kwargs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['SiteID']=self.site_id
context['SiteName']=self.site.location
context['FormType']='Add'
context['active_subnets']='class="active"'
return context
class EditSubnet(UpdateView):
model = SiteSubnets
form_class = SubnetForm
template_name = "sites/subnet_form.html"
@method_decorator(user_passes_test(lambda u: u.has_perm('config.edit_subnet')))
def dispatch(self, *args, **kwargs):
self.site_id = self.kwargs['site_id']
self.site = get_object_or_404(SiteData, pk=self.site_id)
return super(EditSubnet, self).dispatch(*args, **kwargs)
def get_success_url(self, **kwargs):
return reverse_lazy("sites:site_detail_subnets", args=(self.site_id,))
def form_valid(self, form):
form.instance.site_data = self.object.site_data
return super(EditSubnet, self).form_valid(form)
def get_form_kwargs(self, *args, **kwargs):
kwargs = super().get_form_kwargs()
return kwargs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['SiteID']=self.site_id
context['SiteName']=self.site.location
context['FormType']='Edit'
context['active_subnets']='class="active"'
return context
答案 0 :(得分:3)
这是罪魁祸首:form_class = SubnetForm(is_add=True)
。 form_class
应该是名称所指示的内容,只是class
,而不是实例。使用get_form_kwargs
将初始化参数添加到表单构造函数调用中(正如您对site_id
所做的那样):
class AddSubnet(CreateView):
form_class = SubnetForm # just the form CLASS
# ...
def get_form_kwargs(self, *args, **kwargs):
kwargs = super().get_form_kwargs()
kwargs['is_add'] = True # you can set 'is_add' here
kwargs['site_id'] = self.site_id
return kwargs