获取Django中选定多个复选框的值 [英] Get the values for selected multiple checkbox in Django

查看:52
本文介绍了获取Django中选定多个复选框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django构建应用程序.现在我面临复选框问题.我可以从request.POST.getlist(checkbox [])中检索值.但是它带有一个列表.然后,我正在制作一个for循环以使用块来获取价格,但是在这里,我面临着如何将其与每个复选框的单独变量一起存储的问题.因为它是循环的,所以对于具有不同变量的不同值不可行吗?我该怎么办?

I am building an app with django. Now i am facing a problem with checkbox. I can retrive the values from request.POST.getlist(checkbox[]). But its comming with a list. Then i am making a for loop to use the slug to get the prices but here i faced like how to store it with a separate variable for each check box. As it is in loop, for different values with different variables is not possibe? How could i do it ?

在我的模型中,我有一张桌子,上面有其他物品.它具有SSL,安全性,备份.

In my model I have one table with extras. It has SSL, SECURITY, BACKUP.

如果选中了SSL和SECURITY复选框,那么我将获得价格.但我希望将其添加到Order模型,该模型具有SSL和SECURITY之类的字段.

If the check box of SSL and SECURITY selected then by the slug I will get the price. But i want that to add to Order model which has a fields like SSL and SECURITY .

我完全感到困惑.我应该如何建立模型架构.通过托管,用户可以购买SSL,安全性,备份或其中的任何一种.

I am getting totaly confused. How should I make the model architecture. With Hosting user can buy SSL, SECURITY, BACKUP or any of them.

def checkout(request):
    if request.method == "POST":
        extras_slugs = request.POST.getlist("checkbox[]")
        for slug in extras_slugs:

推荐答案

您应在此处使用request.POST.getlist.这是我根据复选框存储考勤数据的示例.

You should use request.POST.getlist here. This is example where I am storing attendance data based on checkbox.

观看次数:

if request.method == "POST":
            id_list = request.POST.getlist('choices')

以html

    <form  action="{% url 'submitattendance' %}" method="post" role="form">
                    {% csrf_token %}

  <table class="table table-hover">
    <thead>
      <tr>
        <th>Name</th>
        <th>Status</th>
        <th><input type="checkbox" align="center" onClick="toggle(this)"></th>
      </tr>
    </thead>
    <tbody>
    {% for attendance in attendances %}
      <tr {% if attendance.present %} style="background-color:green;"{% endif %}>
        <td>{{attendance.first_name}} {{attendance.last_name}}</td>
        <td>{{attendance.status}}</td>
        <td><input type="checkbox" name="choices" value="{{attendance.id}}" {% if attendance.present %} checked="checked"{% endif %} class="checkbox_delete"></td>
        <td><input type="hidden" name="attendances" value="{{attendance.id}}"></td>
       </tr>
    {% endfor %}
    </tbody>
  </table>

希望这会有所帮助.

这篇关于获取Django中选定多个复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆