Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

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

License

Notifications You must be signed in to change notification settings

themotleyfool/django-charsleft-widget

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

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.

##Installation

The package can be installed via:

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

##Usage

There are a few ways of setting a widget for a form field:

###via forms.py

from django import forms
from charsleft_widget.widgets import CharsLeftInput
  
class ExampleForm(forms.Form):
  name = forms.CharField(widget=CharsLeftInput())

or

from django import forms
from charsleft_widget.widgets import CharsLeftInput
  
class ExampleForm(forms.Form):
  name = forms.CharField()

  def __init__(self, *args, *kwargs):
    super(ExampleForm, self).__init__(*args, **kwargs)
    self.fields['name'].widget = CharsLeftInput

via admin.py

from django.contrib import admin
from charsleft_widget.widgets import CharsLeftInput, MediaMixin
    
# The MediaMixin is what loads the css and javascript only one time per admin page
class ExampleAdmin(MediaMixin, admin.ModelAdmin):
  # Use widget on all instances of this form field
  formfield_overrides = {
    models.TextField: {'widget': CharsLeftInput(attrs={
      'maxlength': 200,
      'style': "height: 200px; width: 400px;",
      })
    },
  }

or

from django.contrib import admin
from charsleft_widget.widgets import CharsLeftInput, MediaMixin
  
# The MediaMixin is what loads the css and javascript only one time per admin page
class MyModelAdmin(MediaMixin, admin.ModelAdmin):
  pass
  
  # Use widget on particular instances of the form field
  def formfield_for_dbfield(self, db_field, **kwargs):
    if db_field.name is 'my_field_name':
      kwargs['widget'] = CharsLeftInput(attrs={'size':'add normal attrs here, like field size numbers'})
    return super(ContentObjectAdmin,self).formfield_for_dbfield(db_field,**kwargs)

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 73.4%
  • JavaScript 21.0%
  • CSS 5.6%