librelist archives

« back to archive

numerous selectfields in a html page

numerous selectfields in a html page

From:
Stéphane Guedon
Date:
2014-03-21 @ 09:15
Hi the list !

I am currently developping a small web app to control transmission 
bittorrent daemon.

I get stuck with a problem (in at least two places in the app) : in my 
page, I have several times the same control (a selectfield to modify 
the torrent priority) and I don't know how to manage it.

At first, I had a great form with fiedlist and formfield. That was not 
simple, elegant or whatever.

Every torrent item was opened in the view, every of its attribute 
(name, progress, status AND priority) was placed in a field of a single 
form (one per torrent), and this torrent form was placed in the field 
list. Then the whole gigantic form (which is actually a form list) was 
given to the template.

I would rather prefer give my simple listing of torrents to the 
template, and for each one, the template itself creates a selectfield, 
which can simply send some order to a view function. Is that possible 
in some way ?

Thanks !

Re: [flask] numerous selectfields in a html page

From:
Shawn Milochik
Date:
2014-03-21 @ 14:08
I'm not sure I understand your question. Can you post some code?

I suspect you might benefit from using WTForms:
https://flask-wtf.readthedocs.org/en/latest/

Re: [flask] numerous selectfields in a html page

From:
Jörg Bönisch
Date:
2014-03-22 @ 14:05
Hi,

I cant see class definitions, so I think first you need to define a form 
class with a
class wtforms.fields.SelectField(default field 
arguments, choices=[], coerce=unicode, option_widget=None) in it, as only 
putting some objects into a dict doesnt make a selectfield.

A read on 
http://wtforms.readthedocs.org/en/latest/fields.html#basic-fields should 
help.

Regards, J

Re: [flask] numerous selectfields in a html page

From:
Stéphane Guedon
Date:
2014-03-22 @ 14:12
Le samedi 22 mars 2014, 15:05:00 Jörg Bönisch a écrit :
> Hi,
> 
> I cant see class definitions, so I think first you need to define a
> form class with a class wtforms.fields.SelectField(default field
> arguments, choices=[], coerce=unicode, option_widget=None) in it,
> as only putting some objects into a dict doesnt make a selectfield.

Already done :

class TorrentBandwidth(Form):
	tor_id = HiddenField('tor_id')
	bandwidthpriority = SelectField(u'Torrent priority', choices=[( 
'-1','low'),('0','normal'),('1','high')])
	def __init__(self, *args, **kwargs):
		kwargs['csrf_enabled'] = False
		super(TorrentBandwidth, self).__init__(*args, **kwargs)

> 
> A read on
> http://wtforms.readthedocs.org/en/latest/fields.html#basic-fields
> should help.

As said before, I have read it quite often !


> 
> Regards, J

Re: [flask] numerous selectfields in a html page

From:
Stéphane Guedon
Date:
2014-03-22 @ 12:59
Le vendredi 21 mars 2014, 10:08:01 Shawn Milochik a écrit :
> I'm not sure I understand your question. Can you post some code?

No I can't, cause I am not sure of the current code should be the one 
to use...

Let's show you by some schema :

Torrent name	| control priority
Ubuntu...		| selectfield 'low', 'high'
Debian...		| selectfield ...

And that continues with as many torrent you might have !

Now the problem is : do I try to make torrent names part of a form and 
each form is inside a form list (using formfield and fieldlist).

Or should I try another way I don't know yet ?

I just tried to set a list of forms fields and display them in the html 
page like this :

torrents_forms = dict()
torrents = client.get_torrents(listing)
for torrent in torrents:
	form = TorrentBandwidth(csrf_enabled=False)
	form.tor_id = torrent.hashString
	form.bandwidthpriority = torrent.bandwidthPriority
	torrents_forms[torrent.hashString]=form

and then in html :

<td>
	<p><a href="{{ url_for('torrent', tor_id=torrent.hashString) 
}}">{{torrent.name}}</a></p>
	</td>
	<td>
		{{torrents_forms[torrent.hashString]
(value=torrent.bandwidthPriority)}}
	</td>

but I don't get the selectfields, but only text :

lpic_2_linux_professional_institute_certification_study_guide-
wiley-2011.pdf
<app.forms.TorrentBandwidth object at 0x7fa200f83650>

> 
> I suspect you might benefit from using WTForms:
> https://flask-wtf.readthedocs.org/en/latest/

I am obviously using them quite intensly !

Re: [flask] numerous selectfields in a html page

From:
Shawn Milochik
Date:
2014-03-22 @ 20:22
On Sat, Mar 22, 2014 at 8:59 AM, Stéphane Guedon <stephane@22decembre.eu>wrote:

>
> torrents_forms = dict()
> torrents = client.get_torrents(listing)
> for torrent in torrents:
>         form = TorrentBandwidth(csrf_enabled=False)
>         form.tor_id = torrent.hashString
>         form.bandwidthpriority = torrent.bandwidthPriority
>         torrents_forms[torrent.hashString]=form
>
>
This is putting the forms into a dict, obviously.



> and then in html :
>                 {{torrents_forms[torrent.hashString]
> (value=torrent.bandwidthPriority)}}
>         </td>
>
>
 This is referring to a form, which makes sense with the output you
reported: <app.forms.TorrentBandwidth object at 0x7fa200f83650>

Try  {{torrents_forms[torrent.hashString][fieldname] and see what happens.