简体   繁体   English

Python、Django:编辑 inlineformset_factory

[英]Python, Django: Editing inlineformset_factory

inside my app I'm using an inlineformset_factory and right know it's working fine.在我的应用程序中,我使用的是 inlineformset_factory 并且知道它工作正常。 But when displaying it inside my template the labels are always staying right above the input-field?但是当在我的模板中显示它时,标签总是停留在输入字段的正上方? Is there any way to display them side-by-side or even move the label as some sort of placeholder inside the input-field?有什么方法可以并排显示它们,甚至将 label 作为输入字段内的某种占位符移动?

views.py视图.py

formset = inlineformset_factory(Model_A, Model_B, can_delete=False, extra=0, fields=('fields_01', 'fields_02', 'fields_03'))

template.html模板.html

<form method="post">
    {% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset %}
        {{ form }}
    {% endfor %}
    <button type="submit">Save</button>
</form>

Thanks for all your help and have a great weekend!感谢您的所有帮助,祝您周末愉快!

Try this:尝试这个:

<form action="" method="POST">
                        {% csrf_token %}
                        {{ form.management_form }}
                        {% for field in form %}
                            {{field.product.label}} - {{field.product}}   #here product is my field name
                            <hr>
                        {% endfor %}
                        
                        <input type="submit" name="Submit">
 </form>

You have to do this for every field你必须对每个领域都这样做

#Changes #变化

 <form method="post">
        {% csrf_token %}
        {{ formset.management_form }}
        {% for form in formset %}
            {{ form.Field_name.label }}  -  {{form.Field_name}} #1st field

            {{ form.Field_name.label }}  -  {{form.Field_name}} #2nd field
        {% endfor %}
        <button type="submit">Save</button>
    </form>

I think the displaying was influenced by css。我认为显示是受css影响的。
Use F12 to check css in chrome to find the reason。使用F12在chrome中查看css,查找原因。
You can edit the css to change the displaying.您可以编辑 css 以更改显示。
Or use django-widgets-improved and bootstrap to set class to the fields of form.或者使用 django-widgets-improved 和 bootstrap 将 class 设置为表单字段。

{% load widget_tweaks %}
 <form method="post">
        {% csrf_token %}
        {{ formset.management_form }}
        {% for form in formset %}
            {% for field in form %}
              {% render_field field class="form-control" %}
            {% endfor %}
        {% endfor %}
        <button type="submit">Save</button>
  </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM