简体   繁体   English

在Django中提交后如何保留复选框状态?

[英]How to retain checkbox state after submit in django?

Checkbox has two states(checked and unchecked).In my view I have several products and I am trying to filter it based on its category.When I click on any checkbox, checkbox state is changing(getting unchecked).Also I am unable to select multiple checkboxes. Checkbox具有两种状态(已选中和未选中)。在我看来,我有几种产品,并且正在尝试根据其类别对其进行过滤。当我单击任意复选框时,复选框的状态正在更改(变得未选中)。选择多个复选框。

Models.py, Models.py,

class Add_cat(models.Model):
    category = models.CharField("Name")
    cat_name = models.BooleanField(default=False)

My template file, 我的模板文件

<head>
<script type="text/javascript">
function myfunction(){
    document.getElementById("myform").submit();
}
</script>
</head>
<body>
<form action="{% url 'welcome_user' %}" id="myform">
    {% csrf_token %}
    <p >Categories</p>
    {% for i in My_Cat %}

        <input type="checkbox" name="cat_name" value="{{i.category}}" 
         onclick="return myfunction()" 
    {% if add_cat.cat_name %}checked="checked"{% endif %}>{{i.category}}    

    {% endfor %}
</form>
</body>

Views.py, Views.py,

#Add_prod class contains product list with category as foreign key to Add_cat
def welcome_user(request): 
    categories = Add_cat.objects.all()       
    if 'cat_name'  in request.GET:
        filter_category = request.GET.getlist('cat_name')
        my_products = Add_prod.objects.filter(cat__category__in = filter_category)
        context = { 
                "My_Cat":categories,
                "products":my_products    
        }      

    if 'cat_name' not  in request.GET:
        my_products = Add_prod.objects.all()
        context = { 
                "My_Cat":categories,
                "products":my_products    
        }
    return render(request,"welcome-user.html",context)

Your for loop is iterating with i , but your if uses an add_cat , change one of them to be the same as the other. 您的for循环使用i迭代,但是您的if使用add_cat ,将其中一个更改为另一个。

{% for i in My_Cat %} --> {% for add_cat in My_Cat %}
{% if add_cat.cat_name %}  -->  {% if i.cat_name %}
{% for i in My_Cat %}

    <input type="checkbox" name="{{i.category}}" value="{{i.category}}" 
         onclick="return myfunction()" 
    {% if add_cat.cat_name %}checked="checked"{% endif %}>{{i.category}}    
{% endfor %}

You are using the same name for all the checkbox, so when you change the state of one, you change the state of all you need to use {{i.cat_name}} instead of cat_name . 您为所有复选框使用了相同的名称,因此,更改状态为1时,将更改所有需要使用{{i.cat_name}}而不是cat_name This is the reason for all your checkboxes changing the name when you click in one. 这就是当您单击所有复选框时所有复选框都更改名称的原因。

Btw, I don't know your requirements, but for me, the name of your attributes are quite confuse. 顺便说一句,我不知道您的要求,但是对我来说,属性的名称非常混乱。 I would spend more time to rethink the name of the attributes to avoid more errors like this one in the future. 我将花费更多的时间来重新考虑属性的名称,以避免将来出现更多此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM