Dynamic forms

342 views
Skip to first unread message

Sandeep kaur

unread,
Jul 30, 2012, 3:53:27 PM7/30/12
to django-users
I want to have a form, with drop down list and then multiple select
checkboxes dynamically filtered based on drop down selection. I have
searched a lot but did not find a complete solution for my
requirement. Also I want to ask that can we do this without apply java
query ? Means if a browser doesn't support java, how would our could
run?

Help would be really appreciated.
Thank you.

--
Sandeep Kaur
E-Mail: mkaur...@gmail.com
Blog: sandymadaan.wordpress.com

Nicolas Emiliani

unread,
Jul 30, 2012, 4:55:18 PM7/30/12
to django...@googlegroups.com
On Mon, Jul 30, 2012 at 4:53 PM, Sandeep kaur <mkaur...@gmail.com> wrote:
I want to have a form, with drop down list and then multiple select
checkboxes dynamically filtered based on drop down selection. I have
searched a lot but did not find a complete solution for my
requirement. Also I want to ask that can we do this without apply java
query ?  Means if a browser doesn't support java, how would our could
run?

Well, I just did something alike, where I basically have three dropdowns
that are related , when you pick something in the first one, the others load 
up some different options. 

The way i did it was attaching the change event using jQuery to the rendered
dropdown and the working my way from there. I don't know if it is the most
solid/clean way to do it, but it works. I wouldn't know how to do that without js.

 

Help would be really appreciated.
Thank you.

--
Sandeep Kaur
E-Mail: mkaur...@gmail.com
Blog: sandymadaan.wordpress.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Nicolas Emiliani

Lo unico instantaneo en la vida es el cafe, y es bien feo.

Marshel Helsper

unread,
Jul 30, 2012, 4:58:24 PM7/30/12
to django...@googlegroups.com
The only way I can think of doing it without JS would be by having the users select an option from the drop down and then click a next button, submitting to the server for the server to figure out what to do next.
Thanks,

Marshel

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." - Ben Franklin

George Silva

unread,
Jul 30, 2012, 5:09:16 PM7/30/12
to django...@googlegroups.com
I think that he does NOT want to use jQuery ("java Query").

Without javascript you will need to resubmit data to the server, so he figures out what to do.
George R. C. Silva

Desenvolvimento em GIS
http://geoprocessamento.net
http://blog.geoprocessamento.net

Tomas Neme

unread,
Jul 30, 2012, 5:26:08 PM7/30/12
to django...@googlegroups.com
Just to be clear, sandeep, jQuery is not java, javascript is not java.
Get yourself clear on that, because it can become a big confusion.

Besides that, yes, without javascript, the only way you've got to do
that, is having the user submit a form where he selects the options,
and then return another form with the appropriate options rendered.
Maybe you can use a form wizard for this

--
"The whole of Japan is pure invention. There is no such country, there are
no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

Satinder Goraya

unread,
Jul 31, 2012, 2:29:31 AM7/31/12
to django...@googlegroups.com
On Tue, Jul 31, 2012 at 1:23 AM, Sandeep kaur <mkaur...@gmail.com> wrote:
> I want to have a form, with drop down list and then multiple select
> checkboxes dynamically filtered based on drop down selection. I have
> searched a lot but did not find a complete solution for my
> requirement. Also I want to ask that can we do this without apply java
> query ? Means if a browser doesn't support java, how would our could
> run?
>
> Help would be really appreciated.
Lets say that you need a form with a drop-down list that have dynamic
values. With Django this can be done simple and fast.
MY_CHOICES = (
('1', 'Option 1'),
('2', 'Option 2'),
('3', 'Option 3'),
)

class MyForm(forms.Form):
my_choice_field = forms.ChoiceField(choices=MY_CHOICES)

The form`s class has an __init__ method that is called on every form
load. Most of the times you skipped it in the form definition but now
you will have to use it.

def get_my_choices():
# you place some logic here
return choices_list

class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['my_choice_field'] = forms.ChoiceField(
choices=get_my_choices() )

You first call the __init__ method of the parent class(Form) using the
super keyword and then declare your dynamic fields(in this case
my_choice_field). With this code get_my_choices is called on every
form load and you will get your dynamic drop-down.

--
Satinderpal Singh
http://satindergoraya.blogspot.in/

Sandeep kaur

unread,
Aug 1, 2012, 3:59:53 AM8/1/12
to django...@googlegroups.com
On Tue, Jul 31, 2012 at 11:59 AM, Satinder Goraya
<satinder...@gmail.com> wrote:

> Lets say that you need a form with a drop-down list that have dynamic
> values. With Django this can be done simple and fast.
<snip>
> my_choice_field). With this code get_my_choices is called on every
> form load and you will get your dynamic drop-down.

This code is useful for bringing the drop down options from the database.
But I want to have the dynamically filtered options from the last
selected options without refreshing the page.

I have used the following code but it is not working according to my
requirement.
Please check and point out where I am making mistake.

<views.py >

from django import template

register = template.Library()

@register.inclusion_tag("field_test_select.html")
def field_test_select(request):
field_list = Field.objects.all()
return render_to_response('field_test_select.html', {'field_list'
: field_list}, context_instance=RequestContext(request))

def all_json_tests(request, field):
current_field = Field.objects.get(id=field)
test = Test.objects.all().filter(field=current_field)
json_test = serializers.serialize("json", test)
return HttpResponse(json_test, mimetype="application/javascript")

<urls.py>

(r'^add_job/$', 'field_test_select'),
(r'^field/(?P<field>[-\w]+)/all_json_tests/$', 'all_json_tests'),

<field_test_select.html>

<!-- field_test_select.html -->
<html>
<body>
<form action="" method="get" accept-charset="utf-8">
<select name="field" id="field">
<option value="Z">Select a field</option>
{% for field in field_list %}
<option value="{{ field.id}}">{{ field.name }}</option>
{% endfor %}
</select>
<select name="test" id="test" disabled="true">
<option>Select a test</option>
</select>
</form>
<script>
$(document).ready(
function() {
$("select#field").change(function() {
if ($(this).val() == 'Z') {
$("select#test").html("<option>Select
a test</option>");
$("select#test").attr('disabled', true);
}
else {
var url = "/field/" + $(this).val() +
"/all_json_tests";
var field = $(this).val();
$.getJSON(url, function(tests) {
var options = '<option
value="Z">Select a test</option>';
for (var i = 0; i < tests.length; i++) {
options += '<option value="' +
tests[i].pk + '">' + tests[i].fields['name'] + '</option>';
}
$("select#test").html(options);
$("select#test
option:first").attr('selected', 'selected');
$("select#test").attr('disabled', false);
});
//}
});


$("select#test").change(function(vent) {
if ($(this).val() == -1) {
return;
}
myAwesomeFunctionToCallWhenAtestIsSelected();
});
});
}

</script>
</body>
</html>
Reply all
Reply to author
Forward
0 new messages