Login

Remaining character count in django admin

Author:
timbroder
Posted:
February 1, 2010
Language:
Python
Version:
1.1
Tags:
admin jqeury charactercount
Score:
1 (after 1 ratings)

Want to display the remaining characters on a text field in admin? (based off of maxlength or an override)

also lives here: http://github.com/broderboy/django-admin-remainingcharacters

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#have your ModelAdmin inherit this to use
class CounterAdmin(admin.ModelAdmin):
    counted_fields = ()
    
    #really for textareas
    max_lengths = {'abstract': 400,}
    
    class Media:
        js = ('js/jquery.js',
              'js/jquery.charCount.js',)
        
    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(CounterAdmin, self).formfield_for_dbfield(db_field, **kwargs)
        print db_field.name
        print self.counted_fields
        if db_field.name in self.counted_fields:
            try:
                len = self.max_lengths[db_field.name]
                field.widget.attrs['maxlength'] = len
            except: pass
            field.widget.attrs['class'] = 'counted ' + field.widget.attrs.get('class','')
        return field


"""
jquery,charCount.js

/*
 * 	Character Count Plugin - jQuery plugin
 * 	Dynamic character count for text areas and input fields
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/7161/jquery-plugin-simplest-twitterlike-dynamic-character-count-for-textareas
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

/*
 * Modified 2010 Tim Broder for django-admin-charcount
 * http://gpowered.net
 */
 
(function($) {
	$.fn.charCount = function(options){
		// default configuration properties
		var defaults = {	
			allowed: 140,		
			warning: 20,
			css: 'help',
			counterElement: 'p',
			cssWarning: 'warning',
			cssExceeded: 'exceeded',
			counterText: ''
		}; 
			
		var options = $.extend(defaults, options); 
		
		function calculate(obj){
			var count = $(obj).val().length;
			var available = options.allowed - count;
			if(available <= options.warning && available >= 0){
				$(obj).next().addClass(options.cssWarning);
			} else {
				$(obj).next().removeClass(options.cssWarning);
			}
			if(available < 0){
				$(obj).next().addClass(options.cssExceeded);
			} else {
				$(obj).next().removeClass(options.cssExceeded);
			}
			$(obj).next().html(options.counterText + available);
		};

		this.each(function() {  			

			$(this).after('<'+ options.counterElement +' class="' + options.css + '">'+ options.counterText +'</'+ options.counterElement +'>');
			calculate(this);
			$(this).keyup(function(){calculate(this); });
			$(this).change(function(){calculate(this)});
		});
	  
	};

})(jQuery);

/*function init_counters(selector, len){
	$(selector).each(function() {
		//console.log($(this).attr('maxlength'));
		if(len==null){
			len = $(this).attr('maxlength');
		}
		$(this).charCount({
			counterText: 'Characters Remaining: ',
			allowed: len,
		});
	});
}*/

$(document).ready(function(){
	$(".counted").each(function(){
		console.log($(this));
		len = $(this).attr('maxlength');
		$(this).charCount({
			counterText: 'Characters Remaining: ',
			allowed: len,
		});
	});
	//init_counters("input[maxlength]", 80);
	//init_counters("textarea[maxlength]");
	//init_counters("#id_abstract", 400);
});

/*
 * <style type="text/css">
form .counter{
	}
form .warning{color:#600;}	
form .exceeded{color:#e00;}
</style>
*/
"""

More like this

  1. Django Admin inline preview by timbroder 5 years, 5 months ago
  2. Group results by a range of dates in admin sidebar with calendar by coolchevy 4 years, 5 months ago
  3. Admin Input Field Character Count via jQuery by joshman 6 years, 6 months ago
  4. Persistent Session Debugging with Django Debug Toolbar by brianjaystanley 3 years, 8 months ago
  5. Improved command to generate UML diagrams of whole project or specified apps by nikolajusk 4 years, 9 months ago

Comments

Please login first before commenting.