Skip to main content

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

Project description

django-colorfield

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

django-colorfield-hex django-colorfield-hexa


Installation

  • Run pip install django-colorfield
  • Add colorfield to settings.INSTALLED_APPS
  • Run python 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:

from colorfield.fields import ColorField
from django.db import models

class MyModel(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.

from colorfield.fields import ColorField
from django.db import models

class MyModel(models.Model):
    color = ColorField(format="hexa")

image_field

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

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

from colorfield.fields import ColorField
from django.db import models

class MyModel(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 the samples option.

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

django-colorfield-samples

from colorfield.fields import ColorField
from django.db import models

class MyModel(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 palette
    color = ColorField(choices=COLOR_PALETTE)

Forms

Model forms

The colorfield.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 specified format (e.g., hex, rgb, rgba).

from django import forms

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ["color"]

Plain forms

The colorfield.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.

from django import forms
from colorfield.forms import ColorField

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

Admin

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


Testing

# clone repository
git clone https://github.com/fabiocaccamo/django-colorfield.git && cd django-colorfield

# create virtualenv and activate it
python -m venv venv && . venv/bin/activate

# upgrade pip
python -m pip install --upgrade pip

# install requirements
pip install -r requirements.txt -r requirements-test.txt

# install pre-commit to run formatters and linters
pre-commit install --install-hooks

# run tests
tox
# or
python runtests.py
# or
python -m django test --settings "tests.settings"

Credits

Originally developed by Jared Forsyth


License

Released under MIT License.


Supporting

  • :star: Star this project on GitHub
  • :octocat: Follow me on GitHub
  • :blue_heart: Follow me on Twitter
  • :moneybag: Sponsor me on Github

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. 🧟‍♂️

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_colorfield-0.14.0.tar.gz (37.0 kB view details)

Uploaded Source

Built Distribution

django_colorfield-0.14.0-py3-none-any.whl (34.8 kB view details)

Uploaded Python 3

File details

Details for the file django_colorfield-0.14.0.tar.gz.

File metadata

  • Download URL: django_colorfield-0.14.0.tar.gz
  • Upload date:
  • Size: 37.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_colorfield-0.14.0.tar.gz
Algorithm Hash digest
SHA256 478dbd3975a88f2ea2a9afc325faaca05c54ebf04ec985ce130f6dea39dfb899
MD5 bc2734a1dee08ffadc00b0330d5e5936
BLAKE2b-256 494adcc1c9a071cf7e32391139843d4eb127a91bf9cbfe19596f924d51cb1680

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_colorfield-0.14.0.tar.gz:

Publisher: create-release.yml on fabiocaccamo/django-colorfield

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_colorfield-0.14.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_colorfield-0.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f169c4e7ad8f336e51d4ea81f866346e7d4f336f3766e54e144cd16ea7d84a0e
MD5 acc1df161fe3d7207ef4ac38e0fad36a
BLAKE2b-256 e0631226d92ec38286a7c1d41959fb2fb5ba4dd68e4a179d3554c478e8ee6c92

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_colorfield-0.14.0-py3-none-any.whl:

Publisher: create-release.yml on fabiocaccamo/django-colorfield

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page