I'm trying to implement the HTML5 colorpicker in Django's admin page.
Here is my model:
#model.py ... class Category(models.Model): ... color = models.CharField(max_length=7)
Here is the form:
#form.py from django.forms import ModelForm from django.forms.widgets import TextInput from .models import Category class CategoryForm(ModelForm): class Meta: model = Category fields = '__all__' widgets = { 'color': TextInput(attrs={'type': 'color'}), } class CategoryAdminForm(ModelForm): form = CategoryForm
And finally the admin:
#admin.py ... from .forms import CategoryAdminForm ... class CategoryAdmin(admin.ModelAdmin): form_class = CategoryAdminForm filter_horizontal = ('questions',) fieldsets = ( (None, { 'fields': (('name', 'letter'), 'questions', 'color') }), )
However, the type for the field is still text. How do I change the type for the input field to color in the admin page?
Restart your application server and now you should be able to see a new color scheme added by the package. If you want to customize it further go to your admin and look for “themes” app and do customizations there. Rounded corners for a slight step towards “modern” design.
The <input type="color"> defines a color picker. The default value is #000000 (black). The value must be in seven-character hexadecimal notation.
To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).
I found the answer in the documentation:
The extra class in forms.py was not necessary
#form.py from django.forms import ModelForm from django.forms.widgets import TextInput from .models import Category class CategoryForm(ModelForm): class Meta: model = Category fields = '__all__' widgets = { 'color': TextInput(attrs={'type': 'color'}), }
And in the admin:
#admin.py ... from .forms import CategoryForm ... class CategoryAdmin(admin.ModelAdmin): form = CategoryForm filter_horizontal = ('questions',) fieldsets = ( (None, { 'fields': (('name', 'letter'), 'questions', 'color') }), )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With