【问题标题】:Make a loop to display a blank form or pre-filled form循环显示空白表格或预填表格
【发布时间】:2016-06-09 13:19:12
【问题描述】:

我希望当我的数据库中没有答案时,我可以循环显示一个空白表单。

目前我有这个代码:

 <form method="POST" action="">
    {{ formset.management_form }} {% csrf_token %}
    <table>

      {% for question in questions %}<hr>

    <label for="question">{{ question }} [{{ question.id }}]</label>  
    <input type="hidden" id="id_form-{{ forloop.counter0 }}-question" name="form-{{ forloop.counter0 }}-question" value="{{ question.id }}"/>

  </p>

  {% for reply in question.reply_set.all %}
    <p>
    <label for="answer">Réponse :</label>
    <input type="text" id="id_form-{{ forloop.parentloop.counter0 }}-answer" name="form-{{ forloop.parentloop.counter0 }}-answer" value="{{ reply.answer }}"/>
    <input type="hidden" id="id_form-{{ forloop.parentloop.counter0 }}-id" name="form-{{ forloop.parentloop.counter0 }}-id" value="{{ reply.id }}"/>
    </p>
  {% endfor %}

{% endfor %}
 </table>
    <center><input type="submit" value="Submit" class="btn btn-success" />
    <a href="../../baseVisite/" class="btn btn-success">Retour</a></center>
  </form>

此表格允许我更改问题的答案,但如果我的数据库中已有答案... 如果还没有答案,我会循环显示空白表格

我该如何做这个循环?

【问题讨论】:

    标签: python django forms django-forms django-templates


    【解决方案1】:

    您可以为此使用for...empty 模板标签。

    {% for reply in question.reply_set.all %}
        <p>
            <label for="answer">Réponse :</label>
            <input type="text" id="id_form-{{ forloop.parentloop.counter0 }}-answer" name="form-{{ forloop.parentloop.counter0 }}-answer" value="{{ reply.answer }}"/>
            <input type="hidden" id="id_form-{{ forloop.parentloop.counter0 }}-id" name="form-{{ forloop.parentloop.counter0 }}-id" value="{{ reply.id }}"/>
        </p>
    {% empty %}
    
        {# your blank form goes here #}
    
    {% endfor %}
    

    【讨论】:

    • empty 很好,但问题是当它向我显示它没有保存的空白字段时.. 你知道为什么吗?我应该更改 forloop.counter 吗?
    • 我不太明白你的问题。你能用这个查询更新问题吗?
    • 现在可以了,非常感谢您的帮助!这是工作!
    【解决方案2】:
    {% if question.reply_set.all %}
    {% for reply in question.reply_set.all %}
    <p>
      <label for="answer">Réponse :</label>
      <input type="text" id="id_form-{{ forloop.parentloop.counter0 }}-answer" name="form-{{ forloop.parentloop.counter0 }}-answer" value="{{ reply.answer }}"/>
      <input type="hidden" id="id_form-{{ forloop.parentloop.counter0 }}-id" name="form-{{ forloop.parentloop.counter0 }}-id" value="{{ reply.id }}"/>
    </p>
    {% endfor %}
    {% else %}
    <p>
      <label for="answer">Réponse :</label>
      <input type="text" id="id_form-{{ forloop.parentloop.counter0 }}-answer" name="form-{{ forloop.parentloop.counter0 }}-answer"/>
      <input type="hidden" id="id_form-{{ forloop.parentloop.counter0 }}-id" name="form-{{ forloop.parentloop.counter0 }}-id"/>
    </p>
    {% endif %}
    

    基本上它会检查是否有任何答案,如果有则显示它们,如果没有则显示一个空的答案表单。

    【讨论】:

    • 也感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2021-08-19
    相关资源
    最近更新 更多