Django获取所有选中或未选中的复选框值 [英] Django gets all checkboxes values, checked or un-checked

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

问题描述

使用Django的 User 模型,我有以下模板:

Using Django's User model, I have the following template:

更新:更详细的模板

<form method=POST action="...">
  <table>
    ...
    {% for account in accounts %}
      <tr>
        <td>
          {{ account.username }}
        </td>
        <td>
          <input type=checkbox name=account value="{{ account.id }}" {% if account.is_active %}checked{% endif %}>
        </td>
      </tr>
    {% endfor %}
  </table>
<input type=submit value=Submit>
</form>

{% for pg in paginator.page_range %}
  {% if queryset.number == pg %}
    <li><span>{{ pg }}</span></li>
  {% else %}
    <li><a href=".../?page={{ pg }}">{{ pg }}</a></li>
  {% endif %}
{% endfor %}

显示每个帐户的状态,用户可以更新它们.

which shows the status of each account and the user may update them.

更新:包括GET和POST处理.由于有数千个帐户,因此使用Django的 pagination 显示它们.

UPDATE: including GET and POST handling. As there are thousands of accounts, they are displayed using Django's pagination.

视图中:

def account(request):
    if request.method == 'GET':
        users = User.objects.all()
        paginator = Paginator(users, 30)
        page = request.GET.get('page')
        accounts = paginator.page(page)
        ...

        return render(request, 'account/account.html', {'accounts':accounts, 'paginator':paginator}

    # POST
    accounts = request.POST.getlist('account')    # This will get only the `checked` ones
    for account in User.objects.filter(id__in=accounts):
        if account is checked:    # Only hypothetical ...
            account.is_active = True
        else:
            account.is_active = False
        account.save()
    return redirect(...)

问题:

  1. 如何检索已选中或未选中的所有复选框?

检索到的列表如何包含每个帐户的状态,以便我可以相应地设置帐户的 is_active 字段?

How can the retrieved list contains the status of each account, so that I can set the account's is_active field accordingly?

推荐答案

1..要取消选中,可以使用 .exclude

1. To get un-checked, you can using .exclude

User.objects.exclude(id__in=accounts)

更多指定;

account_ids = [u.id for u in User.objects.exclude(id__in=accounts)]

2..将帐户的帐户设置为 is_active .

2. set the account's as is_active or not.

User.objects.filter(id__in=accounts).update(is_active=True)
User.objects.exclude(id__in=accounts).update(is_active=False)

我认为您需要做的是:如果选中了帐户,则设置为 is_active = True ,其他设置为 is_active = False ,这是您的正确点吗?

I think what exacly you need to do is: If accounts is checked then set to is_active=True and the others is_active=False, that's your point right?

所以,这是我的建议;

So, this a suggestion from me;

def account(request):
    users = User.objects.all()
    template_name = 'account/account.html'

    if request.method == 'POST':
        account_ids = request.POST.getlist('account')

        users.filter(id__in=account_ids).update(is_active=True)
        users.exclude(id__in=account_ids).update(is_active=False)

        return redirect(...)

    paginator = Paginator(users, 30)
    page = request.GET.get('page')
    accounts = paginator.page(page)
    ....

    context = {'accounts': accounts, 'paginator': paginator}
    return render(request, template_name, context)

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

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