4

I'm trying to add some conditional logic in my Django model form, and it's much more difficult than I'd anticipated. What I want to do is : if a user selects option A, I'd like one more question to be shown, and if they choose option B, I'd like another question to be shown.

I found this answer -- this is exactly what I want, it says to start with making the fields that have optional values (i.e. not required) and then use Javascript to hide the fields, according to the logic. But it doesn't explain how.

I think I can figure out the JS bit but what I'd like to understand is how and where to add this. I'd assume in forms.py, in a widget attrs maybe? I'd love an implementation sample if that's possible. Thanks a lot in advance!

2
  • You could take a look at django-dynamic-forms. Commented Feb 23, 2016 at 15:45
  • Javascript goes in the html page template. Commented Feb 23, 2016 at 16:18

1 Answer 1

5

As you've already mentioned, the most simple way to do what you want is to create a form with all fields (required & optional) and to make some of them be invisible until you want them appear (by selecting some option in other field for example). To do this you should:

  1. Create a form with all fields you want in forms.py.

    Class MyForm(forms.Form):
        #fields go here
    
  2. Pass the form to a desired template.

    def myview(request):
        form = MyForm()
        c = {'form': form}
        return render(request, 'mytemplate.html', c)
    
  3. Render the form in a template.

    <form action="/your-name/" method="post">
        {% csrf_token %}
        {{ form }}
        <input type="submit" value="Submit" />
    </form>
    
  4. Now all magic goes in a .js file. It likely should be in /static/js/ folder and can be named as you wish. Select the fields you want to make invisible by id (jQuery will make it easier to select fields in some other manner, by class for example) and hide them with .hide() method. Alternatively, you may want to hide necessary fields in css by setting their visibility to hidden. At last the only thing you should do is to write a condition in .js file to check if some required field is checked or what option is selected and show some optional hidden fields with .show() method. Imagine, you have a checkbox with id="mycheckbox":

    <script type="text/javascript">
        if (document.getElementById('mycheckbox').checked) {
            document.getElementById('someotherfield').show();
        }
    </script>
    

Of course, there are other ways to make dynamic forms.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, chem1st. I've done steps 1, 2 and 3. How do I give the fields the id? If it's a CharField with 2 choices, say, "yes" and "no", how do I set the logic "if the answer is yes, do this"?
Django forms already have built-in ids - check this out.
The problem was that I didn't realize django forms had built-in ids, and all the while I was wondering how to get the js working! Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.