On Tue, Jul 31, 2012 at 11:59 AM, Satinder Goraya <satinder.goray...@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> -- Sandeep Kaur E-Mail: mkaurkha...@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-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.