I fixed the Memory Error. I accidentally put field_count = 1
inside my loop so it kept resetting itself.
The rest apparently works just fine. Thanks for the help!
Making a variable name to check if it's defined in Python
-
20-07-2023 - |
Pregunta
I have a form where you have the option to add upload inputs. I want the images that will be uploaded with every input to be grouped together (there will be text in between). Every time an upload button is added it get's the name "upload_image1", "upload_image2", ...
I want to check if these names are defined so i can then loop through them later. I am trying to combine upload_image and an integer that is counting up together but it looks like he is trying to add that integer to the value of upload_image which is not defined.
if len(form["upload_image1"]) > 0:
while 1:
field_count = 1
if len(form["upload_image" + str(field_count)]) == 0: break
upload_field = form["upload_image" + str(field_count)]
upload_image += upload_field
article_content += """
<p>%s</p>
""" % (description[field_count].value)
for item in upload_field:
article_content += '<img src="http://www.******.com/images/%s/%s">' % (link_title, item.filename)
field_count = field_count + 1
Solución 2
Otros consejos
Here is how to avoid the KeyError:
- If
form["upload_image1"]
is a string, then useform.get("upload_image1", "")
instead. - If
form["upload_image1"]
is a sequence (e.g. list or tuple), then useform.get("upload_image1", ())
instead.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow