Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Form Building / Django request.POST help

I'm not sure if I'm just form building impaired or not thinking about this the right way. I'm attempting to build a form similar to Gmail's 'compose' form that has an AJAX image uploader. I have a portion of code that uploads the image and returns an image ID working fine. Once I receive the image ID back I've tried appending it to my form in a hidden checkbox field. I want to allow users to upload multiple images, and I'd also like to keep all my hidden checkboxes the same name so I can easily iterate over the values. Essentially this:

Client side (this is from Gmail but on mine these will be hidden):

<input type="checkbox" checked="" value="125e6e5e7d8a2601_125e6e5e7d8a2601_0.2_-1" name="attach" id=":4s"/>
<input type="checkbox" checked="" value="125e6e5e7d8a2601_125e6e5e7d8a2601_0.1_-1" name="attach" id=":50"/>

Server side:

       for picture_id in request.POST["attach"]:
            #do stuff here with the picture_id

Unfortunately I only receive one of the picture_ids and request.POST["attach"] iterates over it like a string. I'm not sure how to resolve my issue and send all the image IDs without using something like a form <select> where multiple items can be selected because I'd have to manually add items and select them.

Hopefully this explanation is clear, I'm sure I'm just missing something trivial. Thanks for the help in advance!

like image 203
Jordan Messina Avatar asked Dec 31 '09 23:12

Jordan Messina


People also ask

Should I use Django form or HTML form?

If you're creating a form in which the data goes into a database table, Django's form is easier as well. The data is easily transferrable from form to database table with Django forms. If you creating any type of complex form that creates complex styling, it's easier to go with plain HTML forms.


1 Answers

If you're expecting a list for the key attach, you should use request.POST.getlist('attach'). Doing request.POST['attach'] will only give you the last value, which is a string.

Happy new year!

like image 131
elo80ka Avatar answered Sep 27 '22 17:09

elo80ka