Attribute Address getting displayed instead of Attribute Value

Posted by Manish on Stack Overflow See other posts from Stack Overflow or by Manish
Published on 2014-06-02T15:15:57Z Indexed on 2014/06/02 15:25 UTC
Read the original article Hit count: 204

Filed under:
|
|

I am try to create the following. I want to have one drop down menu. Depending on the option selected in the first drop down menu, options in second drop down menu will be displayed. The options in 2nd drop down menu is supposed by dynamic, i.e., options change with the change of values in first menu.

Here, instead of getting the drop down menus, I am getting the following Choose your Option1: Choose your Option2:

Note: I strictly don't want to use javascript.

home_form.py

class HomeForm(forms.Form):
    def __init__(self, *args, **kwargs):
        var_filter_con = kwargs.pop('filter_con', None)
        super(HomeForm, self).__init__(*args, **kwargs)
        if var_filter_con == '***':
            var_empty_label = None
        else:
            var_empty_label = '    '

        self.option2 = forms.ModelChoiceField(queryset = db_option2.objects.filter(option1_id = var_filter_con).order_by("name"),
                                        empty_label = var_empty_label,
                                        widget = forms.Select(attrs={"onChange":'this.form.submit();'})
                                        )

        self.option1 = forms.ModelChoiceField(queryset = db_option1.objects.all().order_by("name"),
                                        empty_label=None,
                                        widget=forms.Select(attrs={"onChange":'this.form.submit();'})
                                        )

view.py                                     

def option_view(request):
    if request.method == 'POST':
        form = HomeForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            if cd.has_key('option1'):
                f = HomeForm(filter_con = cd.get('option1'))
                return render_to_response('homepage.html', {'home_form':f,}, context_instance=RequestContext(request))
        return render_to_response('invalid_data.html', {'form':form,},  context_instance=RequestContext(request))
    else:
        f = HomeForm(filter_con = '***')
        return render_to_response('homepage.html', {'home_form':f,}, context_instance=RequestContext(request))


homepage.html

<!DOCTYPE HTML>
<head>
    <title>Nivaaran</title>
</head>
<body>
    <form method="post" name = 'choose_opt' action="">
    {% csrf_token %}
        Choose your Option1:
            {{ home_form.option1 }}
        <br/>
        Choose your Option2:
            {{ home_form.option2 }}
    </form>
</body>

© Stack Overflow or respective owner

Related posts about python

Related posts about html