根据其他字段自定义表单字段

时间:2017-02-27 10:20:26

标签: python django

请给我以下问题的建议。我在这句话的底部添加了相应的脚本。提前谢谢!!!

我想做什么:根据数据库中指定的目标字段自定义表单字段,如下图所示。

enter image description here 现状
Ecms表中的所有单选按钮都列在一列中 enter image description here

html文件

{% extends "base.html" %}
(% load widget_tweaks %}

{% block content %}
    <form method="post" enctype="multipart/form-data">
        <h4 style="margin-top: 0">Project Upload</h4>
        {% csrf_token %}
        {% for hidden in form.hidden_fields %}
            {{hidden}}
        {% endfor %}

        {% for field in form.visible_fields %}
            <div class="form-group">
                <label for="{{field.id_for_label}}">{{field.label}}</label>
                {{field}}
            </div>
        {% endfor %}

        <button type="submit">Upload</button>
    </form>
{% endblock %}

Model.py(父)

class html(models.Model):
    project = models.CharField(max_length=50, blank=True)
    version = models.IntegerField(default=0)
    ecms=models.ManyToManyField(ecm, blank=True)
    diff = models.TextField(blank=True)
    PROGRAM_CHOICES = (
        ('Office', 'General office'),
        ('Residential', 'Residential'),
        ('Retail', 'Retail'),
        ('Restaurant', 'Restaurant'),
        ('Grocery', 'Grocery store'),
        ('Medilcal', 'Medilcal office'),
        ('Research', 'R&D or laboratory'),
        ('Hotel', 'Hotel'),
        ('Daycare', 'Daycare'),
        ('K-12', 'Educational,K-12'),
        ('Postsecondary', 'Educational,postsecondary'),
        ('Airport', 'Airport'),
        ('DataCenter','Data Center'),
        ('DistributionCenter','Distribution center,warehouse')
    )
    program = models.CharField(max_length=20, choices=PROGRAM_CHOICES, default='Retail')
    LOCATION_CHOICES = (
        ('Beijing', 'Beijing'),
        ('China', 'China'),
        ('Hong Kong', 'Hong Kong'),
        ('Japan', 'Japan'),
        ('Shanghai', 'Shanghai'),
        ('Shenzhen', 'Shenzhen'),
        ('Taiwan', 'Taiwan'),
        ('USA', 'United States')
    )
    location = models.CharField(max_length=15, choices=LOCATION_CHOICES, default="Hong Kong")
    CERTIFICATE_CHOICES = (
        ('LEED_v3', 'LEED_v3'),
        ('LEED_v4', 'LEED_v4'),
        ('BEAM+', 'BEAM+'),
        ('WELL', 'WELL'),
        ('No certificate','No certificate')
    )
    certificate = models.CharField(max_length=20, choices=CERTIFICATE_CHOICES, default='BEAM+')
    user = models.CharField(max_length=20, default='test')
    html = models.FileField(upload_to=dir_path)
    uploaded_at = models.DateTimeField(auto_now_add=True)

Model.py

class ecm(models.Model):
    name = models.CharField(max_length=50, blank=True)
    TARGET_CHOICES = (
        ('Heating', 'Heating'),
        ('Cooling', 'Cooling'),
        ('Fan', 'Fan'),
        ('Lighting', 'Lighting'),
        ('Equipment', 'Equipment'),
        ('Renewable','Renewable Energy'),
        ('Hot Water','Hot Water System'),
        ('Others', 'Others'),
    )
    target=models.CharField(max_length=20, choices=TARGET_CHOICES, default='Others')
    description = models.TextField(blank=True)
    link=models.URLField(blank=True)

    def __str__(self):
        return self.name

views.py

def model_form_upload(request):
    if request.method == 'POST':
        if form.is_valid():
            newhtml = form.save()
            newhtml.save()
    else:
        form = DocumentForm()

    return render(request, 'model_form_upload.html', {'form': form})

1 个答案:

答案 0 :(得分:0)

你应该render your fields manually,而不是迭代它们。我建议将所需的每个列包装到div中,并将宽度设置为生成的div。