python - How to filter price range for products in django? -


i trying filter products list price specified user(min , max price).i have 2 input box taking price range.'price' 1 of column in database table.i getting error int() argument must string or number, not 'dict'.i have include template file , small part of views file.

models.py,

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.protect)      def __unicode__(self):         return "%s" % (self.cat) 

my template file,

<p>price</p> <input type="text" name="min_price" maxlength="4" size="3" > <input type="text" name="max_price" maxlength="4" size="3">  <input type="submit" value="go"> 

views.py,

@csrf_protect   def welcome_user(request):      if 'min_price' in request.get:         filter_price1 = request.get.get('min_price')         filter_price2 = request.get.get('max_price')         if filter_price1 =='':             filter_price1=0         if filter_price2=='':             filter_price2=add_prod.objects.all().aggregate(max('price'))         my_products = add_prod.objects.filter(price__range=(filter_price1,filter_price2))         context = { "products":my_products}    return render(request,"welcome-user.html",context) 

i tried this,

my_products = add_prod.objects.raw('select * books_add_prod price between filter_price1 , filter_price2') 

maybe line wrong filter_price2=add_prod.objects.all().aggregate(max('price')) cause aggragate return dict

see docs aggragation

try this: my_products=add_prod.objects.filter(price__range(filter_price1,filter_price2['price_max']))


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -