简体   繁体   English

如何在Django中将输入HTML与formset一起使用

[英]How can I use input HTML with formset in Django

I have a formset with 2 fields. 我有一个包含2个字段的表单集。 When I run it, display this "/>" on the screen. 当我运行它时,在屏幕上显示此“ />”。 Look above my html code. 在我的html代码上方看。 I don´t know why this is happen. 我不知道为什么会这样。 I think that is something in HTML tag and I have to change it. 我认为这是HTML标记中的内容,我必须对其进行更改。 I appreciate if someone can help me to solve this situation. 我很高兴有人可以帮助我解决这种情况。 Thanks. 谢谢。

HTML:

{% load static %}

<!DOCTYPE html>

<body>

<script src="{% static 'id15/assets/js/jquery.min.js' %}"></script>
<script src="{% static 'id15/assets/js/jquery.formset.js' %}"></script>

<form class=" bd-form-20 " action="" name="form-name" method="POST" >
{% csrf_token %}

           <script>
            function caps(element){
                element.value = element.value.toUpperCase();
            }
            </script>

    <label>outorgado</label><br><br>
          <div>
            {{ formsetoutorgado.management_form }}
            {% for formset in formsetoutorgado %}
              <div class="link-formset">

               <input type="text" id={{formset.nom_outorgado}}  /><br><br>
               {{formset.nom_outorgado.errors}}
               <input type="text" id={{formset.num_cpf_outorgado}} /><br><br>
               {{formset.num_cpf_outorgado.errors}}

              </div>
            {% endfor %}
          </div>


        <script>
        $('.link-formset').formset({
                    addText: 'Adicionar',
                    addCssClass: 'add-row',
                    deleteText: 'Remover',
                    deleteCssClass: 'show',
        });
        </script>

    <br>
    <button type = "submit" >
    Cadastrar e Continuar
    </button>

</form>

</body>
</html>

This <input type="text" id={{formset.nom_outorgado}}/> is your problem. <input type="text" id={{formset.nom_outorgado}}/>是您的问题。 {{formset.nom_outorgado}} is actually the <input> field, not the id of the form field. {{formset.nom_outorgado}}实际上是<input>字段,而不是form字段的id So you should either have just: 因此,您应该要么只是:

{{ formset.nom_outorgado }} 

in your HTML (without the surrounding <input ...> ) or if you want to create your HTML manually: 在您的HTML中(没有周围的<input ...> ),或者如果您想手动创建HTML:

<input type="text" id="{{ formset.nom_outorgado.id_for_label }}" name="{{ formset.nom_outorgado.html_name }}" value="{{ formset.nom_outorgado.value }} /> 

to get the id of the field. 获取该字段的id Don't forget the " around the id and to set a name and value if you want to populate the field from the form. As Daniel commented, this is not why Django was made, the first option is what you should do. 如果您想从表单中填充字段,请不要忘记id周围的"并设置namevalue 。正如Daniel所言,这不是创建Django的原因,第一种选择是您应该做的。

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

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