自定义django表单模板(在引导程序中)

时间:2017-12-18 14:05:55

标签: python django twitter-bootstrap forms

我想自定义我的django表单。 例如,在我的代码中,我有工作时间设置,我喜欢这样的格式:

mon_start mon_end
tue_start tue_end
....

但是它会创建一个表单

mon_start 
mon_end
tue_start 
tue_end
....

这是我不想要的输出视图 enter image description here

以下是我的代码:

forms.py

class CompanyWorkingHoursSettingsForm(forms.ModelForm):
    mon_start = forms.TimeField()
    mon_end = forms.TimeField()
    tue_start = forms.TimeField()
    tue_end = forms.TimeField()

    class Meta:
        model = Workinghours
        fields = ("mon_start","mon_end","tue_start","tue_end")

workinghourssettings.html

{% extends 'project/base.html' %}
{% load bootstrap3 %}

{% block page %}
  <div class="col-lg-12">
    <div class="panel">
      <div class="panel-heading bg-blue">
        <h4 class="panel-title text-center text-white">
          Working Hours Settings
        </h4>
      </div>
        <div class="panel-body">         
          <form method="POST" enctype="multipart/form-data">
            {% csrf_token %}            
            {% bootstrap_form company_workinghourssettings_form %}
            <button type="submit" class="btn btn-pink">Update</button>
          </form>

        </div>
      </div>
    </div>
{% endblock %}

如何为上面的表单字段生成自定义排列的表单? (在引导程序中)

2 个答案:

答案 0 :(得分:0)

根据评论中的建议,您可以尝试逐个渲染字段,并应用bootstrap网格:

<form>

    <div class="row">

        <div class="col-md-6">{{ my_form.field_1 }}</div>
        <div class="col-md-6"{{ my_form.field_2 }}></div>

    </div>
    ....


</form>

答案 1 :(得分:-1)

如果问题出在占位符上,您可以使用

mon_start =forms.TimeField(label='the label', widget=forms.TextInput(attrs={'placeholder': 'What ever you want here'}))