简体   繁体   中英

IndexError :tuple index out of range

I have a dictionary which contains a list and the list elements are supposed to be tuple

I am creating a product filter from filter.py I am sending the tuple as per the user choice in URL and in views.py

qu=dict(request.GET)
#qu={'price':['(300,600)']}

I want to run the following query

result=Product.objects.all()

for key, value in qu.items():
    result=result.filter(attribute__price__range=value)

print (result)

I am getting IndexError :tuple index out of range

Try using the ast module.

Ex:

import ast
qu={'price':['(300,600)']}
for value in qu['price']:
    result=result.filter(attribute__price__range=ast.literal_eval(value))

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