Snippet List
Simple filter that truncates string to specific number of letters. Example usage in template:
`{{ myvariable|truncatestring:20 }}`
if myvariable is "That is my long string", the result will be: "That is my long s...".
Put the code into templatetags/.
If you need to upload Image files into folders qualified with user name eg.: 'images/user1/2008/01/01' then you may use this snippet.
In order to use it you have to install ThreadLocals middleware as described here:
http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
Then just import UserImageField class at your models.py and specify 'upload_to' parameter with '%(user)s' in the path eg.:
`
image3 = UserImageField(_('Image 3'),
upload_to='flower_images/%(user)s/%Y/%m/%d',
null=True,
blank=True)
`
This snippet allows you to use YUI's autocomplete widget in a easy way.
1. Download YUI (http://developer.yahoo.com/yui/) library and put it into MEDIA folder (in my case I put YUI/build/ directory as base/yui/ in my MEDIA folder)
2. Create lookup view for your autocomplete field.
See 'test_ajax_ac' function to see how this lookup view may be built. You have to define JsonResponse somewhere in your files. JsonResponse is taken from: http://www.djangosnippets.org/snippets/154/
3. Define url for newly created view in urls.py (in usual way)
4. Include necessary .js and .css files in your page (see example in test_ajax.html)
5. Assign widget to a field - see form's __init__ at UserForm in the example.
Additional (optional) parameters are: format_result_fname (name of javascript function for formatting results - see YUI docs for examples)), item_select_handler_fname (name of javascript function for handling item select event (see YUI docs)).
When using YUI take care about proper skin - you'll possibly need to define wrapper like:
`<div class="yui-skin-sam">....</div>`
around your html code.
- javascript
- yui
- autocomplete
- widget
This widget uses: [DHTML Calendar Widget](http://www.dynarch.com/projects/calendar/).
It is very simple implementation but may be easily extended/changed/refined.
1. Necessary files:
First download calendar package and extract it to your MEDIA folder (MEDIA/calendar/...)
You'll also need a small gif that will be shown as a button that allows user to display calendar. By default this 'gif' is searched at '[MEDIA]images/calbutton.gif' but you may change this path in the code (calbtn variable). You need to download or create callbutton.gif image by yourself (it is not included).
2. Include css and js files in your page (as shown in the comment in the code).
3. In form code assign a widget to a field as usual (see newforms documentation for more details).
4. It is possible to change date format by specifying different value for 'dformat' attribute of widget class.
If you get javascript errors while trying to open calendar try to use english translation file (calendar-en.js). I've found that some translations, eg. Polish, are broken by default. In this case you should override your language translation with english one and translate it by yourself (it is easy).
- datetime
- date
- calendar
- widget
- dhtml
pigletto has posted 4 snippets.