Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 22, 2005 01:49:08

K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form Value Array


I'm converting a PHP application over to django and one thing I took
for granted was PHPs conversion of inputs with the name "input" into
an array automatically. Since django nor python seem to support that
conversion, what do people suggest for receiving 1 to N inputs on a
request.POST?

In my specific case, it's a shopping cart, so I want to let the user
add multiple items at the same time. Eg, in HTML:

<select name="item"><options......></select>
<select name="item"><options......></select>

and ideally I could just do:
items = request.POST.get_list('item')

any ideas? Should I just plan on doing:

pat = re.compile("^.+\$")
values =
for name, value in request.POST.items():
if(pat.search(name)):
values.append(value)

Thanks.

Offline

#2 Nov. 22, 2005 02:24:09

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form Value Array


On 11/21/05, Kevin <> wrote:
> I'm converting a PHP application over to django and one thing I took
> for granted was PHPs conversion of inputs with the name "input" into
> an array automatically. Since django nor python seem to support that
> conversion, what do people suggest for receiving 1 to N inputs on a
> request.POST?

Django certainly supports that! Just use request.POST.getlist(). That
method is guaranteed to return a list.

The docs are here:http://www.djangoproject.com/documentation/request_response/#querydict-objectsExample:

<select name="item" multiple="multiple"><options......></select>

Python code:

items = request.POST.getlist('item')

No ugly "" and "" are needed. :-D

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#3 Nov. 22, 2005 18:04:08

K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form Value Array


Unfortunately, I believe they're necessary. My exact code is a list of
radio inputs, in template lanaguage and radio buttons use the input
name as a way of grouping.

My Models would be:
class OptionSet(Model):
name = CharField()

class Option(Model):
name = CharField
option_set = ForeignKey(OptionSet)

clas Item(model):
options = ForeignKey(OptionSet)

{% for option_set in item.get_options %}

<h1>{{option_set.name}}</h1>
{% for option in option_set.get_choices %}
<input type="radio" name="option"
value="{{option.id}}"> {{option.name}}
{% endfor %}
{% endfor %}

Which once translated would look like:

Color
--------
o Blue
o Red
o Green

Size
------
o Small
o Medium
o Large

Offline

#4 Nov. 22, 2005 18:11:35

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form Value Array


On 11/22/05, Kevin <> wrote:
> {% for option_set in item.get_options %}
>
> <h1>{{option_set.name}}</h1>
> {% for option in option_set.get_choices %}
> <input type="radio" name="option"
> value="{{option.id}}"> {{option.name}}
> {% endfor %}
> {% endfor %}

In this example, there's nothing forcing you to use the square
brackets. You could just use a hyphen:

<input type="radio" name="option-{{option_set.id}}">

...or no character at all:

<input type="radio" name="option{{option_set.id}}">

All that matters is that the view code that processes this knows what
format to expect.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#5 Nov. 22, 2005 18:37:14

K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form Value Array


I understand, and that's what I'm really doing at the moment, but I was
just curious if there's a better way to retrieve these POST values than
just:

options =
for name, value in request.POST.items():
if(name.startswith("option")):
options.append(value)

Offline

#6 Nov. 22, 2005 21:25:37

Luke P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form Value Array


On Tue, 22 Nov 2005 10:37:08 -0800 Kevin wrote:

>
> I understand, and that's what I'm really doing at the moment, but I
> was just curious if there's a better way to retrieve these POST
> values than just:
>
> options =
> for name, value in request.POST.items():
> if(name.startswith("option")):
> options.append(value)
>

You can simplify that with Python's list comprehensions:

options =


Luke

--
"Outside of a dog, a book is a man's best friend... inside of a dog,
it's too dark to read."

Luke Plant || L.Plant.98 (at) cantab.net ||http://lukeplant.me.uk/

Offline

#7 Nov. 22, 2005 21:43:24

K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Form Value Array


I decided to try writing a generic array converter as a middleware:

import re
class ParamsMiddleware:
patt = re.compile("^(\w*)\$")
LARGE_INT = 2**30

def process_request(self, request):
if(request.POST):
post = request.POST.copy()
new_vals = {}
for name, value in post.items():
match =
ParamsMiddleware.patt.match(name)
if not match:
continue
value = post
del post.data
new_name = match.group(1)
try:
idx = int(match.group(2))
except ValueError:
# just append it to the end
# se use the largest number for
sorting
idx =
ParamsMiddleware.LARGE_INT
vals = new_vals.setdefault(new_name,
)
vals.append((idx, value))
if(new_vals):
for name, vals in new_vals.items():
vals.sort(key=lambda x: x)
post = [x for x in
vals]
post._mutable = False
request._post = post
return None

Offline

Board footer

Moderator control

Enjoy the 22nd of April
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support