Django Crispy_forms 没有加载正确的布局
Django Crispy_forms doesn`t load the correct layout
我花了几个小时试图调试这个错误,但找不到原因。我没有任何错误消息。
我想使用清晰的表单布局在两列中显示我的表单。
目标: 使用 forms.py 文件中定义的布局显示表单
问题: 布局未按预期显示,而是在不同的行上列出了所有字段。这是表格 ibb 的屏幕截图。co/nrwRmzT
forms.py
from crispy_forms.helper import FormHelper
from .models import Project
from django.core.exceptions import ValidationError
from crispy_forms.layout import Layout, Submit, Row, Column, ButtonHolder
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ['type','chantier','description','company','contact','po_number','display_po','upload_po','proposal_ref','budget','system','notes']
labels = {
"po_number": "Client PO Ref",
"company": "Customer"
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.layout = Layout(
Row(
Column('type', css_class='form-group col-md-2 mb-0'),
Column('chantier', css_class='form-group col-md-2 mb-0'),
Column('description', css_class='form-group col-md-3 mb-0'),
Column('system', css_class='form-group col-md-2 mb-0'),
css_class='form-row'
),
Row(
Column('company', css_class='form-group col-md-3 mb-0'),
Column('contact', css_class='form-group col-md-3 mb-0'),
css_class='form-row'
),
Row(
Column('proposal_ref', css_class='form-group col-md-2 mb-0'),
Column('budget', css_class='form-group col-md-5 mb-0'),
css_class='form-row'
),
Row(
Column('po_number', css_class='form-group col-md-2 mb-0'),
Column('upload_po', css_class='form-group col-md-2 mb-0'),
Column('proposal_ref', css_class='form-group col-md-2 mb-0'),
css_class='form-row'
),
Row(
Column('notes', css_class='form-group col-md-20 mb-0'),
css_class='form-row'
),
Submit('submit', 'Sign in')
)
Createproject.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% crispy form %}
{% block body %}
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% crispy form %}
</form>
{% endblock %}
非常感谢
在您的 Createproject.html
中试试这个
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
</form>
希望这能解决您的问题。
您需要将 Row() 数据分配给 helper.layout
def __init__(self, *args, **kwargs):
super(ProjectForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_method = 'POST'
self.helper.layout = Layout(
Row(
Column('type', css_class='form-group col-md-6 mb-0'),
[...]
我花了几个小时试图调试这个错误,但找不到原因。我没有任何错误消息。
我想使用清晰的表单布局在两列中显示我的表单。
目标: 使用 forms.py 文件中定义的布局显示表单 问题: 布局未按预期显示,而是在不同的行上列出了所有字段。这是表格 ibb 的屏幕截图。co/nrwRmzT
forms.py
from crispy_forms.helper import FormHelper
from .models import Project
from django.core.exceptions import ValidationError
from crispy_forms.layout import Layout, Submit, Row, Column, ButtonHolder
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ['type','chantier','description','company','contact','po_number','display_po','upload_po','proposal_ref','budget','system','notes']
labels = {
"po_number": "Client PO Ref",
"company": "Customer"
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.layout = Layout(
Row(
Column('type', css_class='form-group col-md-2 mb-0'),
Column('chantier', css_class='form-group col-md-2 mb-0'),
Column('description', css_class='form-group col-md-3 mb-0'),
Column('system', css_class='form-group col-md-2 mb-0'),
css_class='form-row'
),
Row(
Column('company', css_class='form-group col-md-3 mb-0'),
Column('contact', css_class='form-group col-md-3 mb-0'),
css_class='form-row'
),
Row(
Column('proposal_ref', css_class='form-group col-md-2 mb-0'),
Column('budget', css_class='form-group col-md-5 mb-0'),
css_class='form-row'
),
Row(
Column('po_number', css_class='form-group col-md-2 mb-0'),
Column('upload_po', css_class='form-group col-md-2 mb-0'),
Column('proposal_ref', css_class='form-group col-md-2 mb-0'),
css_class='form-row'
),
Row(
Column('notes', css_class='form-group col-md-20 mb-0'),
css_class='form-row'
),
Submit('submit', 'Sign in')
)
Createproject.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% crispy form %}
{% block body %}
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% crispy form %}
</form>
{% endblock %}
非常感谢
在您的 Createproject.html
中试试这个<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
</form>
希望这能解决您的问题。
您需要将 Row() 数据分配给 helper.layout
def __init__(self, *args, **kwargs):
super(ProjectForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_method = 'POST'
self.helper.layout = Layout(
Row(
Column('type', css_class='form-group col-md-6 mb-0'),
[...]