How to retain checkbox state after submit in django?
<p>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.</p>

<p>Models.py,</p>

<pre><code>class Add_cat(models.Model):
    category = models.CharField("Name")
    cat_name = models.BooleanField(default=False)
</code></pre>

<p>My template file,</p>

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

        &lt;input type="checkbox" name="cat_name" value="{{i.category}}" 
         onclick="return myfunction()" 
    {% if add_cat.cat_name %}checked="checked"{% endif %}&gt;{{i.category}}    

    {% endfor %}
&lt;/form&gt;
&lt;/body&gt;
</code></pre>

<p>Views.py,</p>

<pre><code>#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)
</code></pre>