Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HTML5 color picker in Django admin

Tags:

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?

like image 497
shefHauwanga Avatar asked Oct 04 '16 18:10

shefHauwanga


People also ask

How do I change the admin color in Django?

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.

How do you pick a color in HTML?

The <input type="color"> defines a color picker. The default value is #000000 (black). The value must be in seven-character hexadecimal notation.

How use Django admin panel?

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).


1 Answers

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')             }),         ) 
like image 92
shefHauwanga Avatar answered Sep 30 '22 03:09

shefHauwanga