【问题标题】:What is id value in the following code?What is exactly id generating?Does it generates choice 1 or only 1? [closed]以下代码中的 id 值是什么?id 究竟是什么生成的?它是生成选择 1 还是只生成 1? [关闭]
【发布时间】:2020-01-22 08:31:07
【问题描述】:
{% for choice in question.choice_set.all %}

        <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">

        <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}

【问题讨论】:

    标签: python html django


    【解决方案1】:

    假设 question.choice_set.all 有 4 个项目。

    [
        {"id": 0, "choice_text": "Choice 1"},
        {"id": 1, "choice_text": "Choice 2"},
        {"id": 2, "choice_text": "Choice 3"},
        {"id": 3, "choice_text": "Choice 4"},
    ]
    

    生成的代码将如下所示:

    <input type="radio" name="choice" id="choice1" value="0">
    <label for="choice1">Choice 1</label><br>
    <input type="radio" name="choice" id="choice2" value="1">
    <label for="choice2">Choice 2</label><br>
    <input type="radio" name="choice" id="choice3" value="2">
    <label for="choice3">Choice 3</label><br>
    <input type="radio" name="choice" id="choice4" value="3">
    <label for="choice4">Choice 4</label><br>
    

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 1970-01-01
      • 2016-09-09
      • 2010-09-10
      • 2016-04-01
      • 1970-01-01
      • 2010-11-08
      • 2012-03-06
      相关资源
      最近更新 更多