Ich versuche, meine Produktliste nach Preis zu filtern, der vom Benutzer spezifiziert wird (minimaler und maximaler Preis) .Ich habe zwei Eingabefeld für das Nehmen der Preisspanne. 'Preis' ist eins der Spalte in meiner Datenbanktabelle. Ich bekomme Störung als int() Argument muss eine Zeichenfolge oder eine Zahl, nicht 'dict'.Ich habe meine Vorlagendatei und kleinen Teil der Sichten-Datei enthalten.Wie Filter Preisspanne für Produkte in Django?
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)
Meine Template-Datei,
<p>Price</p>
<input type="text" name="min_price" maxlength="4" size="3" >
to <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)
ich auch so versucht,
my_products = Add_prod.objects.raw('SELECT * FROM books_add_prod where price between filter_price1 and filter_price2')
'filter_price1 = request.GET.get ('MIN_PRICE')' 'Druck filter_price1 ' –
Sein korrektes Drucken im Unicode-Nummernformat – Bhanukiran