4

I'm trying to build a conditional form where selecting one option will cause a new field to appear underneath, while selecting another option will display something else.

Formstack has a good example https://www.formstack.com/features/conditional-logic

I couldn't find any preexisting form packages for django with this functionality. How should I start implementing this?

3
  • 2
    Make the fields optional and then use javascript to render your fields based on change event of the referred field. Commented Jul 14, 2015 at 22:19
  • Could I then use django's backend to administer the fields? Commented Jul 14, 2015 at 22:35
  • Yes, of course. just make sure you have the name of the fields right. Commented Jul 14, 2015 at 22:46

1 Answer 1

5

Django forms (especially if you use the ModelForm library) are a direct reflection of your Django application Model. You should therefore start by refactoring your Django application Model to have fields that have optional values (i.e. they can be NULL, empty or have a default value already created).

These would be the form fields that are shown/hidden based on your conditional(s) and they may or may not have values (if they are hidden based on a conditional it is impossible to provide a value to them so the Model fields must be able to accept NULL values or use the defaults).

You would then use a client-side language such as Javascript (JS) to handle the user iteraction with your Django application. A simple to use JS framework like jQuery would be worthwhile investigating for your needs.

In addition to the exceptional Django docs on Forms, I also highly recommend you take a look at Django Crispy Forms writtten by PyDanny to see how Django forms should be done right.

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

Comments

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.