Newbie: How to use object related to dropdown selection in django/javascript spaguetti code?

26 views
Skip to first unread message

Samuel Lerning

unread,
Sep 13, 2009, 8:04:22 PM9/13/09
to Django users
Hello,

I'm a newbie struggling with a simple problem for some time now.
I know it is a simple problem and it is a shame that I don't know how
to solve it.

I have a django spaguetti code that, instead of using forms, uses
javascript functions to get a selection from a selection list
(<select></select>).

Since I have some deadlines, I'm thinking on use request.POST (saw
some
about it on results from google), but I don't know when, where an how
to use
request.POST to get the selection and work on it on the view.

Basicaly, there are a Foo class on Models.py

And I need to get the selected "foo" object from the dropdown to,
then, list
data relatated to that foo in the same page.

The relevants parts of the code are the following:


---->foo_relatory.html (template)

<form method="POST" action="" id="filters">
<tr>
<td>
{% if show_foos_filter %}
<span id="foos" style="text-align: right;">
Foo:&nbsp;
<select id="foos_combo" onChange="filter();">
{% for foo in foos %}
<option value="{{ foo.id }}">{{ foo.nome }}</option>
{% endfor %}
</select>
<input type="hidden" name="foo" id="input_foo">
</span>
{% endif %}
</td>
</tr>
</form>

<script>
function filter(){
var formOK = false;

{% if show_foos_filter %}
formOK = CheckFoo();
{% endif %}

if (formOK == true) {
submitForm('filters');
}
}



function submitForm(formID){
var f = document.getElementById(formID);
f.submit();
}


function CheckFoo(){
combo = document.getElementById('foos_combo');
document.getElementById("input_foo").value = combo.options
[combo.selectedIndex].value;
return true;
}

</filter>



---->views.py

def foo_relatory(request):
#
#
# some python code here
#
return render_to_response('foo_relatory.html',
{'title' : title,
'show_filter_foos': True,
},
context_instance=RequestContext(request))


Thanks in advance,

Samuel.

Tiago Serafim

unread,
Sep 13, 2009, 8:31:14 PM9/13/09
to django...@googlegroups.com
Hi,

First you should have an attribute "name" on your select. It's needed to pass the value when the form is submitted.

If your select's name is "foos_combo", then your code you'll look like this:

def foo_relatory(request):

    if request.method == 'POST':
        foos_combo = request.POST['foos_combo']
        return HttpResponse(foos_combo)
       
    else:

        return render_to_response('foo_relatory.html',
                         {'title'       : title,
                          'show_filter_foos': True,
                         },
                         context_instance=RequestContext(request))
   

HTH,
   

Samuel Lerning

unread,
Sep 14, 2009, 10:11:42 AM9/14/09
to Django users
Hi,

Thank you so much Serafim!
I think this is the right direction. And that was a nice tip. :)

However, now, everytime I select a different foo from the list, a
blank page with a number (that I think is the id from the foo object)
is rendered.

At the moment, the view code has the following structure:

def foo_relatory(request):

<<python code to initialize variables required for correct template
rendering>>

if request.method == 'POST':
foos_combo = request.POST['foos_combo']
return HttpResponse(foos_combo)

else:
return render_to_response('foo_relatory.html',
{'title' : title,
'show_filter_foos': True,
},
context_instance=RequestContext(request))


Well, I'll be working to understand and fix this.
However, if anyone have some sugestions... I'll be happy to hear :)

TIA,

Samuel.

Samuel Lerning

unread,
Sep 14, 2009, 6:03:47 PM9/14/09
to Django users
Hi again,

Some progress here, but a minor problem remaining.
On selecting a foo, the page is re-rendered with the correct data
related to the selected foo.

However, the selection list (dropdown) always shows the name of the
first foo, instead of showing the name of the selected foo.
And I need it to display the name of the selected foo, after
selection.

Any help will be appreciated.

At the moment, the code looks like it:

----------------------->foo_relatory.html

<form method="POST" action="" id="filters">
<tr>
<td>
{% if show_foos_filter %}
<span id="foos" style="text-align: right;">
Foo:&nbsp;
<select id="foos_combo" name="foos_combo" onChange="filter();">
</script>

-------------------------------->views.py

def foo_relatory(request):
.
.
.
.
selected_foo = Foo.objects.get(id=1)

if request.method == 'POST':
id_selected_foo = request.POST['foos_combo']
selected_foo = Foo.objects.get(id=id_selected_foo)
.
.
.
.
Reply all
Reply to author
Forward
0 new messages