Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🎨 color field for django models with a nice color-picker in the admin.

License

NotificationsYou must be signed in to change notification settings

fabiocaccamo/django-colorfield

Repository files navigation

django-colorfield

simple color field for your models with a nice color-picker in the admin-interface.

django-colorfield-hexdjango-colorfield-hexa


Installation

  • Runpip install django-colorfield
  • Addcolorfield tosettings.INSTALLED_APPS
  • Runpython manage.py collectstatic
  • Restart your application server

Usage

Settings

This package doesn't need any setting.

Models

Just add color field(s) to your models like this:

fromcolorfield.fieldsimportColorFieldfromdjango.dbimportmodelsclassMyModel(models.Model):color=ColorField(default='#FF0000')

Field Options

These are the supported custom options:format,image_field,samples

format

The following formats are supported:hex(default),hexa,rgb,rgba.

fromcolorfield.fieldsimportColorFieldfromdjango.dbimportmodelsclassMyModel(models.Model):color=ColorField(format="hexa")

image_field

It is possible to auto-populate the field value getting the color from an image using theimage_field option.

The color will be calculated from thetop-left pixel color of the image each time the model instance is saved.

fromcolorfield.fieldsimportColorFieldfromdjango.dbimportmodelsclassMyModel(models.Model):image=models.ImageField(upload_to="images")color=ColorField(image_field="image")

samples

It is possible to provide a palette of colors to choose from to the widget using thesamples option.

This optionis not restrictive (on the contrary ofchoices option), it is also possible to choose another color from the spectrum.

django-colorfield-samples

fromcolorfield.fieldsimportColorFieldfromdjango.dbimportmodelsclassMyModel(models.Model):COLOR_PALETTE= [        ("#FFFFFF","white", ),        ("#000000","black", ),    ]# not restrictive, allows the selection of another color from the spectrum.color=ColorField(samples=COLOR_PALETTE)# restrictive, it is mandatory to choose a color from the palettecolor=ColorField(choices=COLOR_PALETTE)

Forms

Model forms

Thecolorfield.fields.ColorField can be used in Django model forms (django.forms.ModelForm) to provide a color picker widget.When used in a model form, the field automatically validates the color format based on the specifiedformat (e.g.,hex,rgb,rgba).

fromdjangoimportformsclassMyModelForm(forms.ModelForm):classMeta:model=MyModelfields= ["color"]

Plain forms

Thecolorfield.forms.ColorField can be used in plain Django forms (django.forms.Form) to provide a color-picker widget.This is useful when you need a color input outside of a model context.

fromdjangoimportformsfromcolorfield.formsimportColorFieldclassMyForm(forms.Form):color=ColorField(initial="#FF0000",format="hex")

Admin

The admin will kindly provide a simplecolor picker for all color fields. :)


Testing

# clone repositorygit clone https://github.com/fabiocaccamo/django-colorfield.git&&cd django-colorfield# create virtualenv and activate itpython -m venv venv&&. venv/bin/activate# upgrade pippython -m pip install --upgrade pip# install requirementspip install -r requirements.txt -r requirements-test.txt# install pre-commit to run formatters and linterspre-commit install --install-hooks# run teststox# orpython runtests.py# orpython -m djangotest --settings"tests.settings"

Credits

Originally developed byJared Forsyth


License

Released underMIT License.


Supporting

See also

  • django-admin-interface - the default admin interface made customizable by the admin itself. popup windows replaced by modals. 🧙 ⚡

  • django-cache-cleaner - clear the entire cache or individual caches easily using the admin panel or management command. 🧹✨

  • django-extra-settings - config and manage typed extra settings using just the django admin. ⚙️

  • django-maintenance-mode - shows a 503 error page when maintenance-mode is on. 🚧 🛠️

  • django-redirects - redirects with full control. ↪️

  • django-treenode - probably the best abstract model / admin for your tree based stuff. 🌳

  • python-benedict - dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. 📘

  • python-codicefiscale - encode/decode Italian fiscal codes - codifica/decodifica del Codice Fiscale. 🇮🇹 💳

  • python-fontbro - friendly font operations. 🧢

  • python-fsutil - file-system utilities for lazy devs. 🧟‍♂️


[8]ページ先頭

©2009-2025 Movatter.jp