1

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)

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hey template is being called only on clicking on home menu.When I click on any category template is not being called
How you are doing filter then? are you using any js function for that? You have to do some action for doing this filter. Either you can do this filter using js code based on check box selection or you have to call back-end service for getting filtered response.
0

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)

1 Comment

Hey template is being called only on clicking on home menu.When I click on any category checkbox, template is not being called
0

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"

5 Comments

After checkbox field I included button submit field like this, <button type="submit">Submit</button> now I am getting 404 error like this, 127.0.0.1:8000/welcome_user/%20method%3D?cat_name=Economics
I have included my urls file in the question.Should I make any changes in the url?
add method = post in form , and {% csrf_token %} inside the form content then it will directly post data in to your views
csrf is already there the problem is once I click on submit button after checking in checkbox, it is giving 404 error for my url
check your apps urls or main urls.py
0

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'),

10 Comments

I added button type="submit" after checkbox field.I thought now it will look for action but I am getting page not found for my url. 127.0.0.1:8000/welcome_user/%20method%3D?cat_name=Economics
URL looks wrong. "welcome_user/%20method%3D?cat_name=Economics"It should be "welcome_user/?cat_name=Economics"
Replace "<button type="submit">Submit</button>" with <input type="submit">Submit</input> and <form action="{% url welcome_user %}">
Economics is a checkbox which i clicked and pressed submit
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.