如何从django模板中定义的复选框中检索值

时间:2016-05-12 07:25:41

标签: python django django-templates django-views

我创建了一个包含三个选项的菜单(主页,更改密码和注销)。在家里,我正在显示存储在数据库中的所有书籍。现在我想使用复选框根据书籍类别过滤内容。默认情况下,所有书籍都会在过滤类别后显示,只显示该类别的书籍。但对我来说,即使点击了所有复选框(复选框包含类别),它也会显示所有书籍。我认为存在一些问题在我的模板文件中。

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)

模板文件,

{% 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 个答案:

答案 0 :(得分:1)

要检查checbox值是否处于已发送状态,您应该使用以下条件

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

请记住,in运算符返回boolean,因此您也可以

    filter_category = 'cat_name' in request.GET

为了获得价值,你只需要获得&#39; cat_name&#39;的值。关键如:

    value = request.GET['cat_name']

检查它是否在GET参数中

答案 1 :(得分:0)

我认为您需要做的就是将__in添加到您的过滤器中:

@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)

答案 2 :(得分:0)

复选框与其他表单输入略有不同,因此如果您检查从包含复选框的表单发送的帖子,

如果选中该复选框,您的查询集将如下所示: 价值将在&#39;

所以,你必须检查

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

答案 3 :(得分:0)

请尝试以下代码 的 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)

模板文件 从{%url&#39; welcome_user&#39;删除引号(&#39;) %}

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

<强>更新

views.welcome_user

之间添加引号

urls.py

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