[Answer]-Django Templates/ChoiceField Display Fields based on Selection

1👍

Implementation

After some searching I was able to come up with a solution. I added Dajax to my implementation to reload the page with the required fields based on what type of user the user selected. This enabled me to eliminate the fields that didn’t need to be completed and still perform validation on the ones that did.

Further Reading

To help anyone else that’s having a similar problem I would recommend checking out the Dajax Project, especially the Pagination Example. I ran into some issues by just following the tutorial but if you visit the Dajax Github Repo you should be able to get through it.

Issues To Watch For

  • If you are using JQuery UI insure that you load the JQuery version of Dajax and not the Prototype version. Loading Prototype will result in the JQuery not functioning on the pages that you load the Dajax links in. For reference see the import code located at https://github.com/jorgebastida/django-dajax/issues/37.
  • If you are using JQuery make sure to include the following in your header:
  • <script src="https://github.com/cowboy/jquery-misc/blob/master/jquery.ba-serializeobject.js" type="text/javascript" charset="utf-8"></script>
    

    This is listed in the documentation if you search for it but is easy to overlook

    0👍

    Make different modelforms for each type of profile.
    Get all your forms in the template.
    Finally, use jquery to decide which one should be seen.

    Leave a comment