Skip to content

Dynamically hide/show Django admin form fields using only HTML attributes. No javascript required. ™️

Notifications You must be signed in to change notification settings

scientifichackers/django-hideshow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django hideshow

Dynamically hide/show Django admin form fields using only HTML attributes. No javascript required. ™️

  1. Add js file url to your model admin form's media class -
class MyModelForm(ModelForm):
    class Media:
        js = (
            "https://cdn.jsdelivr.net/gh/scientifichackers/django-hideshow@0.0.1/hideshow.js",
        )          
  1. Declare HTML attributes on any fields you want -
class MyModelForm(ModelForm):
    class Meta:
        widgets = {
            "some_integer_choice_field": forms.Select(
                attrs={
                    # all hidden by default
                    "--hideshow-fields": "a1, a2, a3, a4",
                    # a2, a4 visible when "0" is selected
                    "--show-on-0": "a2, a4",
                    # a1, a2 visible when "1" is selected
                    "--show-on-1": "a1, a2",
                }
            ),
            "some_boolean_field": forms.CheckboxInput(
                attrs={
                    "--hideshow-fields": "b1, b2, b3",
                    # b1, b2 visible if checkbox checked
                    # b3 visible if checkbox un-checked
                    "--show-on-checked": "b1, b2",
                }
            ),
        }   
  1. See it work -

IMAGE ALT TEXT HERE

Full Example

About

Dynamically hide/show Django admin form fields using only HTML attributes. No javascript required. ™️

Resources

Stars

Watchers

Forks

Packages

No packages published