uuid template tag
A simple template tag that generates a random UUID and stores it in a name context variable.
- template-tag
- uuid
A simple template tag that generates a random UUID and stores it in a name context variable.
Formats a django variable using a python string formatting as specified in another template variable. Similar to |stringformat. Takes two arguments: the django template variable with the item to be formatted and the django template variable containing the format string. {% pyformat number formatstringvar %} Place this file in `appname/templatetags/pyformat.py` and you're good.
Python json module or simplejson don't support Decimals out of the box. Here's an encoder that casts Decimals into floats and then displays them in desired format.
My previous snippet with captcha wasn't very portable but Marco Fucci figured out the thing that I couldn't - value_from_datadict function. So all credits go to him and his snippet, I adapted it to my needs, maybe you like my version better - it doesn't need any captcha libraries and let's you modify the widget's html easily. Also I added an option to pass remoteip to google api's verify method. How to use it: In your settings.py add: RECAPTCHA_PUBKEY = 'your recaptcha public key' RECAPTCHA_PRIVKEY = 'your recaptcha private key' After that just import and use ReCaptchaField in your form as you would any other field. That's it. *** Important *** If you want to have peace of mind in case google decided that the remoteip parametr is mandatory then: Derive every form that has the captcha field from ReCaptchaForm and when you create the form object after receiving POST/GET, pass a remoteip parameter like that: form = YourCaptchaForm(data=request.POST, remoteip=request.META['REMOTE_ADDR'])
A template loader useful for writing templates with carefully controlled newlines and spaces while retaining readable template source code formatting. Start the template with PTFTAG (`{#ptfable#}`, here) to allow it to be processed. Common problems with doing it to most templates as-is is use of newlines to separate words and multiple spaces between tags where spaces are still needed (which is problematic with `spaceless` tag as well). Currently intended as a template loader wrapper, and is suggested to be used with cached loader. Example settings.py configuration: _lp = lambda lo, *ar: (lo, ar,) # loader, arguments TEMPLATE_LOADERS = ( _lp('django.template.loaders.cached.Loader', # cache _lp('ptf.template.ptftemplateloader.Loader', # ptf 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', #'django.template.loaders.eggs.load_template_source' ), # ptf ), # cache ) (change `ptf.` to wherever in python path you've put it). You might also need couple of simple template tags for explicitly inserting newlines or whitespaces: def br(): return "\n" br = register.simple_tag(br) # XHTML-XMPP-template compatible. def brx(): return "<br />\n" brx = register.simple_tag(br) def ws(): return " " ws = register.simple_tag(ws) .
**UPDATE**: A more complete example is up on [GitHub](http://github.com/henriklied/django-twitter-oauth/tree/master) Based around Simon Willison's [Fire Eagle oAuth](http://www.djangosnippets.org/snippets/655/), this will allow you to develop Twitter oAuth applications (…that is – if you're in the closed beta)
This is just a very short (and mostly useless on it's own) example of how the built in slugify filter can be used in a Python script to generate slugs. It was pulled from a script I've written to pull in items from Upcoming.org's API. I post it because "sunturi" posted [Snippet #29](http://www.djangosnippets.org/snippets/29/), which duplicates the functionality of the built-in slugify filter. In the comments of that snippet, santuri (and others) seem to believe that template filters can only be used within templates. This is incorrect, and I think it's important people understand they can be used elsewhere. sunturi's snippet does remove prepositions from values before slugifying them, so if you need that, his code will work work. But if all you need is slugification, the built-in slugify filter will work fine -- in a Python script, as well as in a template.
A Django form widget which displays help text for individual items in a set of radio buttons. It overrides the RadioSelect widget, adding a small bit of HTML after each <input> element, with the help text for each item. It was developed for a Django Dash project I'm working on (called transphorm.me), so isn't as feature-rich as it could be, but if you have any trouble installing it - or if I've miscopied any of my code in my rush - please let me know.
If you use javascript code with Django-template filter or other related things, it will be not sufficient to qoute string in javascript. This filter escape the string and quote it.
This is a small and useful decorator that you can use to protect yourself from bad users or bots hitting your site.
Use on forms of your app.
Example model: class MyModel(models.Model): file = RemovableFileField(upload_to='files', \ null=True, blank=True) image = RemovableImageField(upload_to='images', \ null=True, blank=True) A delete checkbox will be automatically rendered when editing such a model using form_for_instance. [UPDATED version which works with ModelForms](http://www.djangosnippets.org/snippets/636/)
This is extremely simple decorator to add possibility to upload files with name specific in some object field. For example image with same name as object slug. Sample **model**: class Test(models.Model): image = models.ImageField(\ upload_to=upload_to_dest(path='pics/', \ human_readable_field='hrname')) hrname = models.CharField( \ max_length=128, \ blank=True, default='') Sample **form** for admin: FNAME_EXP = re.compile('^[A-Za-z0-9\-\_]+$') class TestAdminForm(forms.ModelForm): hrname = forms.RegexField( label="Human Readable File Name", \ regex=FNAME_EXP, \ help_text="""Allowed only latin alphabet (upper and lower cases), underscore and minus characters. PLEASE, DO NOT INCLUDE EXTENSION OF THE FILE. Sample: test-this-file""", \ required=False) class Meta: model = Test Sample *admin.py* for *Test* model: class TestAdmin(admin.ModelAdmin): form = TestAdminForm admin.site.register(Test, TesetAdmin)
Enables cookie based authentication with apache. I needed user authentication for some static files, but couldn't use the method described [here](http://www.djangoproject.com/documentation/apache_auth/) as this prompts the user for his credentials, making him log in twice. There is some overhead in the code, because it runs all request middleware components (only session and auth would be needed). All arguments described in the link above are supported. I use it like this in the apache config: <Location "/protected/location"> PythonPath "['/path/to/proj/'] + sys.path" PythonOption DJANGO_SETTINGS_MODULE myproj.settings PythonOption DjangoPermissionName '<permission.codename>' PythonAccessHandler my_proj.modpython #this should point to accesshandler SetHandler None </Location>
Form fields use the dateutil module [http://labix.org/python-dateutil](http://labix.org/python-dateutil) to parse natural-language input for date and datetime fields. The callback function will replace all date and datetime fields automatically for form_for_model and form_for_instance. **Note**: by replacing the 'form_class' keyword argument instead of just returning the field itself you preserve the 'required' status of the field.