Issue
I would like to iterate over request.files content and check if a key exists , however the key value is : "item_i" , so the key could be one or more : "item_0" , "item_1" "item_2" ... i would like to create a loop and retrive the keys if the keys startwith item_ save them in a list and at the end return the list lenght if it'szero returns an erro no key "item_"
code looks like :
result = []
for (int i = 0; i < request.files.count; i++):
key = Request.Files.GetKey(i)
if key.startswith("item_"):
result.append(key)
if len(result) == 0:
return f" No {item_}key exists !", HTTPStatus.BAD_REQUEST,
PS : i'm using flask , not sure that request.files.count works but the most important is to iterate over all the request.files items many thanks .
Solution
If you stop your route code in the debugger and evaluate type(request.files) you'll see it's a werkzeug.datastructures.ImmutableMultiDict.
If you want to check if a key is in request.files you can just do:
if "item_0" in request.files:
#do something
If you want to get all the keys from request.files you can use request.files.keys()
and to get the number of items in request.files you can use len(request.files)
.
To replicate the code in your question you can use this:
result = list()
for key in request.files.keys():
if key.startswith("item_"):
result.append(key)
Answered By - barryodev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.