【问题标题】:how can I print objects in a model as options如何将模型中的对象作为选项打印
【发布时间】:2021-04-02 11:26:08
【问题描述】:

我的html

 <select multiple class="form-control" name="tags">
         <option value="">{% for tag in Tag %}
                           {{ tag }}
                          {% endfor %}
         </option>
 </select>

我的意见.py

def CreateView(request, pk=None):
    queryset = Tag.objects.all()
    args = {'Tag':queryset}
    return render(request, 'home/create.html', args)

我这样写我的代码,但它打印它们作为一个选项哦,我仍然是一个初学者 提前谢谢你

【问题讨论】:

    标签: python html django api


    【解决方案1】:

    您的 for 循环位于选项标签内,您需要将其放在外侧才能为每个标签创建一个选项

     {% for tag in Tag %}
     <option value="">{{ tag }}</option>
     {% endfor %}
    

    【讨论】:

    • 谢谢你,但有没有办法让价值PK?不是 str 因为表单不接受 str 谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2016-08-21
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多