how to create drop-down list using pylons

23 views
Skip to first unread message

sreepathy reddy

unread,
Jul 4, 2009, 11:13:32 AM7/4/09
to pylons-...@googlegroups.com
how to create drop-down list in pylons and how to assin the drop-down values to mysql tables values

plz send me the code........plz n tq
--
sreepathyreddy.m
sreepat...@gmail.com
09989456431

afrotypa

unread,
Jul 6, 2009, 10:36:20 AM7/6/09
to pylons-discuss
That is a broad question. Perhaps 2 questions I think really.

1. How to create a singleselect field in a template?

2. How to update the database with the value of a selection field when
the form is submitted?

There is some good pylons documentation for that stuff here :-

http://pylonsbook.com/en/1.0/working-with-forms-and-validators.html

On Jul 4, 11:13 am, sreepathy reddy <sreepathyre...@gmail.com> wrote:
> how to create drop-down list in pylons and how to assin the drop-down values
> to mysql tables values
>
> plz send me the code........plz n tq
> --
> sreepathyreddy.m
> sreepathyre...@gmail.com
> 09989456431

afrotypa

unread,
Jul 6, 2009, 1:17:32 PM7/6/09
to pylons-discuss
** I have replied the email you sent also **

Well I can only provide you some pointers on how to get this done -
you will have to take it up from there.....

*Note that this code is not tested and maybe incorrect. However I
think the concept is sound and that really is what you are trying to
figure out*

In your helpers you can use some webhelpers (a python package that
helps with building html form elements) to help with building the
options.

So you need to import these into helpers.py

from webhelpers.html.tags import *

for the table in your html template file (the one rendered to display
the form) you will need something like :-

<table>
<tr><th>country</th><th>state</th></tr>
<tr>
<td>${h.select('country', selected_values=[],
options=c.available_countries)}</td>
<td id='states_td'>${h.select('state', selected_values=[],
options=c.available_states)}</td>
</tr>
</table>


in your controller you will need to assign values to
c.available_countries and c.available_states (or whatever you
specified for 'option' in the h.select()) that facilitate populating
the options in the template

i.e. something like

c.available_countries= countryOptions()
c.available_states= stateOptions(country=country) #available states
depend on the country

and those functions will be defined to return the lists of countries
and states (depending on the current country)

def countryOptions():
return [(1,'USA'), (2,'UK')]

def stateOptions(country=1):
if country=1:
return [(1,'Arkansas').......(33,'Texas')]
else: #country==2!
return [(1,'UK_State_1'), (2,'UK_State_2')]

This is a very simplified example to show how the state options depend
on which country was selected. You will probably replace the hard
coded lists with another list that is populated using rows returned
from a database call in both functions.

The tricky part is changing the state options displayed when the
country is changed. For that you need to know some ajax.

You need to attach an onclick (or onchange) event to the country
select field that.

1. identifies which country was selected
2. makes an ajax call to a controller action (passing it the country
selected). this action might use a data lookup to determine which
states are in the selected country and returns them to the call back
of the ajax call
3. the call back receives the returned data and then replaces the
current available options for the state selection field to correspond
with the new states returned.

I use the Jquery library. So here is what I would do in the javascript
code using jquery:-

function replace_states(data){
$('#states_td')[0].innerHTML=data.new_states; // replace the entire
contents of the outer td element with id states_td
}

$(function() {
$('#country').click(function (event) { // attach the click event
to the country select field
var country=$(this).val();
$.post('/controller/get_new_states', {country:country },
function(data){
replace_states(data); // replace_states is the call
back function that takes the new states and changes them in the html
form
},'json');

});
});


That leaves the controller function get_new_states which returns the
states for the selected country in a dictionary with key new_states:-

Here I am using json to receive and return the data from the browser.
So the controller function will look like so :-

import formencode
from pylons.decorators import jsonify,validate

@jsonify
@validate(schema=formencode.Schema(country=validators.Int()))
def get_new_states(self,country=None):
country=self.form_result['country']
states_q=dbsession.query(State).filter
(State.country_country_id==country)
states={'new_states':h.select('states', selected_values=[],
options=[(state.state_id, state.state_name) for state in states_q])}
return states

Here I am using SqlAlchemy and the State and Country tables are in a
1:M relationship on country_id, where country_country_id (FK) in the
State table references country_id (PK) in the Country table

Anyhow this should give you a bit of an idea of how to proceed.

good luck!

Mike Orr

unread,
Jul 6, 2009, 10:08:00 PM7/6/09
to pylons-...@googlegroups.com
By the way, ``webhelpers.constants`` contains a list of country codes,
US states and territories, Canadian provinces, and UK counties. These
can easily be passed to webhelpers.html.tags.select(), or use the
standard Python procedures to extract derived data.

Other country lists would also be welcome. (In latin-1 charset.)

--
Mike Orr <slugg...@gmail.com>

Paweł Stradomski

unread,
Jul 7, 2009, 7:49:51 AM7/7/09
to pylons-...@googlegroups.com
W liście Mike Orr z dnia wtorek 07 lipca 2009:

> Other country lists would also be welcome. (In latin-1 charset.
Doesn't it limit the list to only countries of Western Europe?

--
Paweł Stradomski

Mike Orr

unread,
Jul 7, 2009, 1:09:37 PM7/7/09
to pylons-...@googlegroups.com
On Tue, Jul 7, 2009 at 4:49 AM, Paweł Stradomski<pstra...@gmail.com> wrote:
>
> W liście Mike Orr z dnia wtorek 07 lipca 2009:
>> Other country lists would also be welcome.  (In latin-1 charset.
> Doesn't it limit the list to only countries of Western Europe?

The module contains string literals pasted from websites that use
latin-1. We need to keep the original format so that we can update
the data in one step. The module serves the large need for address
forms in English-speaking countries. Perhaps separate modules could
be used for other charsets.

--
Mike Orr <slugg...@gmail.com>

Reply all
Reply to author
Forward
0 new messages