【问题标题】:Django - Dividing a modelform into sectionsDjango - 将模型分成多个部分
【发布时间】:2021-12-27 20:46:08
【问题描述】:

我正在制作一个物业管理应用程序,用户可以在其中填写租赁申请。我正在寻找一种方法来将我的表单分成多个部分,例如

个人信息: 物品 物品 物品 物品 出租历史: 物品 物品 物品 物品 就业 物品 物品 项目

我的表格

class ApplicantForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ApplicantForm, self).__init__(*args, **kwargs)
        self.fields['property'].queryset = Properties.objects.filter(is_active=True)

    class Meta:
        model = Applicant
        fields = '__all__'
        exclude = ['application_date']
        widgets = {
            'requested_move_in_date': DateInput(),
            'dob': DateInput(),
            'job_length': DateInput(),

        }
        labels = {
            'requested_move_in_date': 'Requested Move in Date',
            'dob': 'Date of Birth',
            'ssn':  "Social Security Number",
            'job_length': "Job Start Date"
        }

我的模板

{% load crispy_forms_tags %}
{% block content %}
    <div class="jumbotron text-center">
        Application page
    </div>

    <form class="post-form" method="POST">
        {% csrf_token %}
        {{ form|crispy }}
        <button type="submit" class="save btn btn-primary">Submit</button>
    </form>
{% endblock %}

【问题讨论】:

    标签: django django-models django-forms modelform


    【解决方案1】:

    您可以使用django-crispy-forms 中的FormHelper() 来覆盖表单的布局。这只是一个示例,但您可以根据自己的目的对其进行更改以制作部分:

    from crispy_forms.helper import FormHelper
    
    class ApplicantForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            self.helper = FormHelper()
    
            self.helper.layout = Layout(
                Row(
                    Div('requested_move_in_date', css_class='col-12 col-md-4'),
                    Div('dob', css_class='col-12 col-md-4'),
                    Div('job_length', css_class='col-12 col-md-4'),
                ),
                ButtonHolder(
                    Submit('submit', 'Сохранить'), css_class='form-btn-holder'
                )
            )
    
            super(ApplicantForm, self).__init__(*args, **kwargs)
            self.fields['property'].queryset = Properties.objects.filter(is_active=True)
    
        class Meta:
            model = Applicant
            fields = '__all__'
            exclude = ['application_date']
            widgets = {
                'requested_move_in_date': DateInput(),
                'dob': DateInput(),
                'job_length': DateInput(),
    
            }
            labels = {
                'requested_move_in_date': 'Requested Move in Date',
                'dob': 'Date of Birth',
                'ssn':  "Social Security Number",
                'job_length': "Job Start Date"
            }
    

    【讨论】:

    • 我试过这样做,但我根本无法让表单呈现。我最终只是标记了模板中的每个字段
    • 您是否尝试过直接使用答案中的代码?您在渲染过程中遇到了哪些错误?
    • 没有错误,只是表单没有呈现到页面。
    猜你喜欢
    • 2020-12-23
    • 2020-07-10
    • 2020-02-08
    • 2014-01-26
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多