javascriptdjangoformsdjango-crispy-forms

Filtering html form options based on previous input


I'll preface this question by saying I'm not looking for code here, just an indication of whether my approach is correct before I undertake a large effort. That's why it will appear I have yet to try to code it myself.

I'm developing a django site that includes a database of precedents. I want to let certain users upload new decisions using the generic CreateView and a ModelForm built using crispy-forms. I want to make this as easy for users as possible by removing irrelevant options from html <option> fields. The part of the form I'm concerned with looks a bit like:

<select class="input-xlarge select" id="id_decisions" name="clauses">
    <option value="" selected="selected">---------</option>
    <option value="1">2012 Decisions</option>
    <option value="2">2011 Decisions</option>
    ...
</select>
<select class="input-xlarge select" id="id_clauses" name="clauses">
    <option value="" selected="selected">---------</option>
    <option value="1">2012 clause 1</option>
    <option value="2">2012 clause 2</option>
    <option value="3">2011 clause 1</option>
    ...
</select>

When someone selects 2012 decisions from the dropdown I want to filter away all options in the 'clause' option field that don't say '2012'. I suspect this means writing a javascript filter similar to the answer in this question but I don't know any javascript and therefore don't really understand what that code is doing. Before I commit to learning enough javascript to be sure I produce a good solution I just want to be sure my suspicion about how this is done is correct. Is it? Am I missing an obvious way to do this in Python using either Django or crispy-forms?


Solution

  • If you want the dropdown to be filtered without submitting the form and refreshing the page, then the only way to do it is with Javascript. This isn't a limitation of Django: that's just how web applications work.