gpt4 book ai didi

python - 添加自定义布局时,Django Crispy 表单不会保存

转载 作者:行者123 更新时间:2023-12-01 04:41:53 25 4
gpt4 key购买 nike

来自views.py:

class EmployeeUpdateView(LoginRequiredMixin, UpdateView):
model = Employee
template_name = "employee_update.html"
form_class = EmployeeUpdateForm

来自 forms.py:

class EmployeeForm(forms.ModelForm):
departments = DepartmentChoiceField()

def __init__(self, *args, **kwargs):
super(EmployeeForm, self).__init__(*args, **kwargs)

self.helper = FormHelper()
self.helper.form_class = "form-vertical"
self.helper.form_tag = False
self.helper.layout = Layout(
Div(Field('first_name'), css_class="row col-md-5 emp-firstname"),
Div(Field('last_name'), css_class="row col-md-5"),
Div(Field('title'), css_class="row col-md-10"),
Div(Field('email'), css_class="row col-md-10"),
Div(Field('phone_number'), css_class="row col-md-10"),
Div(Field('slack_username'), css_class="row col-md-10"),
Div(Field('department'), css_class="row col-md-5"),
Div(
Div(Field('facility_agent', template="field_checkbox_awesome.html"), css_class="row checkbox checkbox-doorman"),
Div(Field('company_agent', template="field_checkbox_awesome.html"), css_class="row checkbox checkbox-doorman"),
Div(Field('contact_for_company_delivery', template="field_checkbox_awesome.html"), css_class="row checkbox checkbox-doorman"),
Div(Field('contact_for_facility_delivery', template="field_checkbox_awesome.html"), css_class="row checkbox checkbox-doorman"),
css_class="row col-md-5 emp-department"),
Div(Field('mugshot'), css_class="row col-md-10"),
Div(Field('company'), css_class="row col-md-10"),
Div(Field('facility'), css_class="row col-md-10")
)
self.request = kwargs.pop('request', None)
self.fields['first_name'].label = "first name"
self.fields['last_name'].label = "last name"
self.fields['title'].label = "title"
self.fields['email'].label = "email"
self.fields['phone_number'].label = "phone #"
self.fields['slack_username'].label = "slack name"
self.fields['facility_agent'].label = "facility agent"
self.fields['company_agent'].label = "company agent"
self.fields['contact_for_company_delivery'].label = "contact for company delivery"
self.fields['contact_for_facility_delivery'].label = "contact for facility delivery"

class Meta:
model = Employee


class EmployeeCreateForm(EmployeeForm):
def __init__(self, *args, **kwargs):
super(EmployeeCreateForm, self).__init__(*args, **kwargs)


class EmployeeUpdateForm(EmployeeForm):
def __init__(self, *args, **kwargs):
super(EmployeeUpdateForm, self).__init__(*args, **kwargs)

employee = Employee.objects.filter(pk=self.instance.pk).first()
self.fields['departments'].set_company(employee.department_id)

和 urls.py:

url(r'employees/(?P<pk>[0-9]+)/$', EmployeeUpdateView.as_view(), name="employee_update"),

如果我注释掉views.py中的“form_class =”行,则编辑将被保存。如果我把它放进去,编辑就不会。这些字段肯定是通过 POST 发送的,并且控制台上没有记录任何错误。

最佳答案

因为 form_valid 没有被调用,这意味着问题不是您的表单未保存,而是您传递给表单的数据未通过验证。 Take a look here at the docs .

要调试该问题,请首先在 View 中放置 form_invalid() 方法或 custom clean() method在您的表单中,并查看表单未验证的原因。即,检查表单实例中的错误并查看显示的内容。这应该可以回答你的问题。

您还调用了一些方法,这些方法可能会或可能不会使表单在实例化之前无效。我不确定 set_company 方法在字段上执行什么操作,但它可能会出现错误,导致您的表单无法正确设置。

关于python - 添加自定义布局时,Django Crispy 表单不会保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30489776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com