An application to assist with css framework color selections.
Project description
django-colors
A Django app providing customizable color selection fields for your models with Bootstrap color integration.
Overview
Django Colors offers a simple yet powerful way to add color selection capabilities to your Django models. It provides:
- Custom model fields for color selection
- Pre-defined Bootstrap color choices
- Support for both background and text color CSS classes
- Ability to define your own color palettes
- Custom widget for color selection in forms
The app is designed to be flexible, allowing you to use either the built-in Bootstrap color options or define your own custom colors through Django models.
Installation
pip install django-colors
Add 'django_colors' to your INSTALLED_APPS setting:
INSTALLED_APPS = [
...
'django_colors',
...
]
Usage
Basic Usage
Add a color field to your model:
from django.db import models
from django_colors.fields import ColorModelField
class MyModel(models.Model):
name = models.CharField(max_length=100)
color = ColorModelField()
By default, this will use Bootstrap color choices for background colors.
Using Text Colors
To use text colors instead of background colors:
from django_colors.field_type import FieldType
class MyModel(models.Model):
name = models.CharField(max_length=100)
color = ColorModelField(color_type=FieldType.TEXT)
Custom Color Models
Create a model for custom colors:
from django_colors.models import ColorModel
class MyCustomColor(ColorModel):
"""Custom color definitions for my app."""
class Meta:
verbose_name = "Custom Color"
verbose_name_plural = "Custom Colors"
Use your custom colors in a field:
class MyModel(models.Model):
name = models.CharField(max_length=100)
color = ColorModelField(model=MyCustomColor)
Global Configuration
You can configure the app globally in your settings.py:
COLORS_APP_CONFIG = {
'default': {
'default_color_choices': 'django_colors.color_definitions.BootstrapColorChoices',
'color_type': 'BACKGROUND',
},
'my_app': {
'default_color_choices': 'myapp.colors.MyCustomColorChoices',
'color_type': 'TEXT',
},
'my_app.MyModel.color_field': {
'only_use_custom_colors': True,
},
}
Templates
The app includes templates for rendering color selections:
color_select.html- Main template for the color selection widgetcolor_select_option.html- Template for individual color options
You can override these templates in your project by creating your own versions in your templates directory.
API Reference
ColorOption
A dataclass representing a single color option with the following attributes:
value: Unique identifier for the colorlabel: Human-readable labelbackground_css: CSS class for background colortext_css: CSS class for text color
ColorChoices
Base class for defining a collection of color options with methods to:
- Get choices as a list of tuples for Django choice fields
- Look up color options by value
- Iterate over available color options
BootstrapColorChoices
Pre-defined color choices based on Bootstrap's color system, including:
- Primary colors (blue)
- Success (green)
- Warning (yellow)
- Danger (red)
- Various other colors (purple, indigo, pink, etc.)
FieldType
Enum defining the type of color field:
BACKGROUND: For background color CSS classesTEXT: For text color CSS classes
ColorModelField
A custom Django field for selecting colors, with options for:
- Using predefined or custom colors
- Specifying field type (background or text)
- Using a custom model or queryset for color options
ColorModel
Abstract base model for custom color definitions with fields for:
name: Color namebackground_css: CSS class for background colortext_css: CSS class for text color
ColorChoiceWidget
Custom form widget for color selection.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Guidelines
- Code should be properly linted and formatted according to the ruff settings in pyproject.toml
- All tests must pass, and new code must have tests
- Add documentation for new features
- Follow type hinting conventions
- Include docstrings with argument and return type information
Development Setup
- Clone the repository
- Create a virtual environment
- Install development dependencies:
pip install -e ".[dev]" - Run tests:
pytest
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_colors-1.0.0.tar.gz.
File metadata
- Download URL: django_colors-1.0.0.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eca3a83fa0fbdb3a2c72b66229b0258007b8cdcd22ce0f536e18f9a8cf2a15aa
|
|
| MD5 |
13bec67f4021ac95b44167200c183acb
|
|
| BLAKE2b-256 |
bb8ae4dadd92af3f061237a62dd456cb587a6b3ab70025c0531730dc7cccba92
|
Provenance
The following attestation bundles were made for django_colors-1.0.0.tar.gz:
Publisher:
publish.yml on hannylicious/django_colors
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_colors-1.0.0.tar.gz -
Subject digest:
eca3a83fa0fbdb3a2c72b66229b0258007b8cdcd22ce0f536e18f9a8cf2a15aa - Sigstore transparency entry: 208131825
- Sigstore integration time:
-
Permalink:
hannylicious/django_colors@b0c0de601c92d8818fee521cd3d24af83dc79e1f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/hannylicious
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b0c0de601c92d8818fee521cd3d24af83dc79e1f -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_colors-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_colors-1.0.0-py3-none-any.whl
- Upload date:
- Size: 24.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd7aa20972d46b977f52814231147bcab0ffbe0cb8ea4625d5a0fdca33cbb1e9
|
|
| MD5 |
9e68ebd143b24612877049b7110d98cb
|
|
| BLAKE2b-256 |
35025c557e624e85c49f89e1324030ebfb76f00db70cea424bff3a060fbea777
|
Provenance
The following attestation bundles were made for django_colors-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on hannylicious/django_colors
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_colors-1.0.0-py3-none-any.whl -
Subject digest:
fd7aa20972d46b977f52814231147bcab0ffbe0cb8ea4625d5a0fdca33cbb1e9 - Sigstore transparency entry: 208131828
- Sigstore integration time:
-
Permalink:
hannylicious/django_colors@b0c0de601c92d8818fee521cd3d24af83dc79e1f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/hannylicious
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b0c0de601c92d8818fee521cd3d24af83dc79e1f -
Trigger Event:
release
-
Statement type: