如何在 Django 管理中使用 HTML5 颜色选择器 [英] How to use HTML5 color picker in Django admin
本文介绍了如何在 Django 管理中使用 HTML5 颜色选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 Django 的管理页面中实现 HTML5 颜色选择器.
I'm trying to implement the HTML5 colorpicker in Django's admin page.
这是我的模型:
#model.py
...
class Category(models.Model):
...
color = models.CharField(max_length=7)
表格如下:
#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
最后是管理员:
#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?
推荐答案
我在文档中找到了答案:
I found the answer in the documentation:
forms.py 中的额外类不是必需的
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'}),
}
在管理员中:
#admin.py
...
from .forms import CategoryForm
...
class CategoryAdmin(admin.ModelAdmin):
form = CategoryForm
filter_horizontal = ('questions',)
fieldsets = (
(None, {
'fields': (('name', 'letter'), 'questions', 'color')
}),
)
这篇关于如何在 Django 管理中使用 HTML5 颜色选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文