简体   繁体   中英

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

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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