Kiran
Kiran

Reputation: 1531

How to retrieve value from checkbox defined in django template

I have created a menu with three options(home,change password and logout). In home I am displaying all the books stored in the database. Now I want to filter the contents based on categories of the books using checkbox. By default all the books are shown upon filtering the category only the books of that category should be shown.But for me it is showing all the books even after clicking on all the checkbox (checkbox contains the categories).I think there is some problem in my template file.

urls.py,

 url(r'^welcome_user/$',views.welcome_user, name='welcome_user'),

Models.py,

class Add_cat(models.Model):
    category = models.CharField("Name",max_length=25,unique=True)

    def __unicode__(self):
        return u'{0}'.format(self.category)

class Add_prod(models.Model):
    book = models.CharField("Book Name",max_length=40)
    author = models.CharField("Author",max_length=30)
    price = models.PositiveIntegerField("Price")
    image = models.ImageField(upload_to='images',null=True)
    cat = models.ForeignKey(Add_cat,on_delete=models.CASCADE)

Template file,

{% block content %}
{% load staticfiles %}
<head>
<link rel="stylesheet" href="{% static 'style.css' %}">
</head>
<body>
<div class="box">
    <div class="sideNav">
        <form action="{% url 'welcome_user' %}">
            <p id=id3>Categories</p>
            <hr>
            {% for i in products %}
                <input type="checkbox" name="cat_name" value="{{i.cat}}">{{i.cat}}<br>
            {% endfor %}
        <button type="submit">Submit</button>
        </form>
    </div>
    <div>
        {% for i in products %}
            <div style="display:inline-block;margin:30px">
                <img src="{{i.image.url}}" alt="No Image" width=196px height=196px>
                <p id=id4>Rs.{{i.price}}</p>
            </div>
        {% endfor %}
    </div>
</div>
</body> 
{% endblock %}

Views.py,

@login_required   
def welcome_user(request):      
    if 'cat_name' in request.GET:
       filter_category = request.GET.getlist('cat_name')
       my_products = Add_prod.objects.filter(cat__in=filter_category) 
       context = { "products":my_products}
    else:
       my_products = Add_prod.objects.all()     
       context = { "products":my_products}
return render(request,"welcome-user.html",context)

Upvotes: 1

Views: 4464

Answers (4)

Anoop
Anoop

Reputation: 1435

Try this below code Add_prod.objects.filter(cat__in=filter_category)

@login_required   
def welcome_user(request):      
    filter_category = request.GET.getlist('cat_name')

    if filter_category:

        my_products = Add_prod.objects.filter(cat__in=filter_category)
        context = { "products":my_products}
    else:
        my_products = Add_prod.objects.all()
        context = { "products":my_products}
    return render(request,"welcome-user.html",context)

Template file Remove quotes(') from {% url 'welcome_user' %}

<form method='get' action="{% url welcome_user %}">

UPDATE

Add quotes between views.welcome_user

urls.py

url(r'^welcome_user/$','views.welcome_user', name='welcome_user'),

Upvotes: 0

user5662309
user5662309

Reputation:

Checkboxes work a little bit different from other form inputs, so if you examine a post sent from a form that includes a checkbox,

if the checkbox is checked, your queryset will look like: the value will be 'on'

So, you have to check

 if request.POST.get('cb1')=='on': 
        "item is selected"
    else:
        "item is not selected"

Upvotes: 0

m.antkowicz
m.antkowicz

Reputation: 13581

To check whether checbox value is in sent form you should go with following condition

    if 'cat_name' in request.GET:
        #checkbox has been checked
    else:
        #it is not checked

Remember that in operator return boolean so you can also do

    filter_category = 'cat_name' in request.GET

To get value you just need to get values of 'cat_name' key like:

    value = request.GET['cat_name']

after checking whether it is in GET parameters or not

Upvotes: 3

2ps
2ps

Reputation: 15956

I think all you need to do is add __in to your filter:

@login_required   
def welcome_user(request):      
    filter_category = request.GET.getlist('cat_name')
    categories = Add_cat.objects.all()
    if filter_category: # I couldn’t decipher the structure of an `Add_cat`, so I left off the `in` check since it would not work for lists
        print filter_category
        my_products = Add_prod.objects.filter(cat__in=filter_category) # the template implies that this field is `cat` and not `category`
        context = { "products":my_products}
    else:
        my_products = Add_prod.objects.all()
        context = { "products":my_products}
    return render(request,"welcome-user.html",context)

Upvotes: 0

Related Questions