https://github.com/scientifichackers/django-hideshow
Dynamically hide/show Django admin form fields using only HTML attributes. No javascript required. ™️
https://github.com/scientifichackers/django-hideshow
Last synced: 9 months ago
JSON representation
Dynamically hide/show Django admin form fields using only HTML attributes. No javascript required. ™️
- Host: GitHub
- URL: https://github.com/scientifichackers/django-hideshow
- Owner: scientifichackers
- Created: 2020-02-28T18:09:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-28T18:55:07.000Z (over 6 years ago)
- Last Synced: 2025-02-28T22:20:39.939Z (over 1 year ago)
- Language: Python
- Size: 7.81 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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 -
```python
class MyModelForm(ModelForm):
class Media:
js = (
"https://cdn.jsdelivr.net/gh/scientifichackers/django-hideshow@0.0.1/hideshow.js",
)
```
2. Declare HTML attributes on any fields you want -
```python
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",
}
),
}
```
3. See it work -
[](https://www.youtube.com/watch?v=PeQ_uQuaTCI)
[Full Example](myapp/admin.py)