简体   繁体   English

如何在具有多个查询集的Django中创建搜索功能?

[英]How to make a search function in Django with multiple queryset?

Hi i'm having trouble with my search function, I want to get product and quantity items from DB as user inputs data in searchbar... but the problem is that im having getting no results.. any help? 嗨,我的搜索功能遇到问题,我想从数据库中获取产品和数量项目,因为用户在搜索栏中输入了数据...但是问题是我没有得到任何帮助。

views.py views.py

def productsearch(request):
    try:
        product=request.GET.get['product']
        quantity=request.GET.get['quantity']
        product_result=Product.objects.filter(product_id__icontains=product)
        quantity_result=Product.objects.filter(quantity_onhand__icontains=quantity)
        return render_to_response('product/productlocation.html',{'product_result' : product_result,'quantity_result':quantity_result, 'product': product,'quantity':quantity})
    except:
        return render_to_response('product/productlocation.html')

template to show result 显示结果的模板

{% if quantity_result %}
 {% for r in quantity_result %}
{{r.quantity_onhand}} 
{% endfor %}
{% else %}
    <h3>no results</h3>
{% endif %}

search bar 搜索栏

 <form action="{% url 'productsearch' %}" method="get">   
        <label for="from">Product</label>
            <input type="text" name="product" value={{request.GET.product}}> <br /><br />
        <label for="to">Quantity &nbsp;</label>
            <input type="text" name="quantity" value={{request.GET.quantity}}> <br />
        <input type="submit" value="Submit">
    </form>

models.py models.py

class Product(models.Model):
    product_id = models.CharField(max_length=100, primary_key=True)
    product_name=models.CharField("Product Name",max_length=50)
    product_unitprice=models.PositiveIntegerField("Unit Price")
    product_size=models.CharField("Product Size",max_length=10)
    productdetail=models.CharField("Product Detail",max_length=100)
    product_img=models.FileField()
    product_type= models.CharField(max_length=15,choices=product_choice, default='stockable')
    retailer_price=models.PositiveIntegerField()
    wholeseller_price=models.PositiveIntegerField()
    location=models.CharField("Locatiion",max_length=30)
    quantity_onhand=models.CharField("Quantity on Hand",max_length=30)
    product_status=models.CharField(max_length=15,choices=product_statuss, default='Available' )

You have syntax error, should be 您有语法错误,应该是

product=request.GET.get('product')
quantity=request.GET.get('quantity')

instead of 代替

product=request.GET.get['product']
quantity=request.GET.get['quantity']

This is the reason why: 这就是为什么:

try:
    ...
except:
    ...

without specifying error type is very bad idea. 没有指定错误类型是非常糟糕的主意。 You cannot say what error actually occured in your code. 您无法说出代码中实际发生了什么错误。

Also render_to_response is deprecated and you should use render instead of it: 同时不建议使用render_to_response ,而应使用render代替它:

return render(request, 'product/productlocation.html',{'product_result' : product_result,'quantity_result':quantity_result, 'product': product,'quantity':quantity})

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

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