Skip to content

timmygee/django-charsleft-widget

 
 

Repository files navigation

Screenshot

A simple django widget that appends a character count to a text input which is determined by the max_length of that particular field. This only works on text inputs and not text areas (as they don't respect max_length anyway)

Installation

The package can be installed via:

pip install git+https://github.com/timmyomahony/django-charsleft-widget.git

Usage

For the most common usage, first create a model:

from django.db import models

class Song(models.Model):
  title = models.CharField(max_length=100)

then create a custom model form that uses the custom widget class.

from django import forms
from charsleft_widget.widgets import CharsLeftInput

from .models import Song

class SongForm(forms.ModelForm):
  name = forms.CharField(widget=CharsLeftInput())

  class Meta:
    model = Song
    fields = "__all__"

If you are using it in the Django admin, import your custom model form in your ModelAdmin:

from django.contrib import admin

from .models import Song
from .forms import SongForm


@admin.register(Song)
class SongAdmin(admin.ModelAdmin):
    form = SongForm

You can optionally toggle off the color changing feature by initialising the widget (from the example above) with:

  name = forms.CharField(widget=CharsLeftInput(attrs={'change_color': False}))

About

A django widget that displays a normal text input with a remaining character count beside it

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 57.2%
  • JavaScript 17.8%
  • Modelica 14.4%
  • HTML 6.4%
  • CSS 4.2%