Login

All snippets

Snippet List

CountryField (ISO 3166-1)

List of countries based on the ISO 3166-1 standard. List adapated from http://opencountrycodes.appspot.com/python/ This is useful for certain services such as Protx that requires countries in the two letter standard.

  • field
  • country
  • custom-field
  • iso-3166-1
  • protx
Read More

Email obfuscation filter using ROT13

An email address obfuscation template filter based on the ROT13 Encryption function in Textmate's HTML bundle. The filter should be applied to a string representing an email address. You can optionally pass the filter an argument that will be used as the email link text (otherwise it will simply use the email address itself). Example usage: {{ email_address|obfuscate:"Contact me!" }} or {{ email_address|obfuscate }} Of course, you can also use this on hardcoded email addresses, like this: {{ "[email protected]"|obfuscate }}

  • filter
  • javascript
  • email
  • textmate
  • obfuscation
  • rot13
Read More

Get the referer view of a request

Get the referer view of a request. **Example:** def some_view(request): ... referer_view = get_referer_view(request) return HttpResponseRedirect(referer_view, '/accounts/login/')

  • view
  • referer
  • request
  • path
Read More

TwitterBackend

TwitterBackend is the twitter authentication backend. This backend makes use of OAuth based "Sign-in with Twitter" Configure your settings.py as per [Django - Specifying Authentication Backends](http://docs.djangoproject.com/en/dev/topics/auth/#specifying-authentication-backends)

  • django
  • authentication
  • python
  • backend
  • twitter
  • oauth
Read More

Simple Admin-button linking to Databrowse

If you want all the objects in your models' admin interface to have a direct link to their databrowse page (just like the *history* link).. follow these steps: 1) copy the *change_form.html* admin template in mytemplates/admin/ 2) edit *change_form.html* by adding this code right next to (before or after) the following line (=the history button markup): `<li><a href="history/" class="historylink">History</a></li>` 3) Make sure that all of you models are registered with [databrowse](http://docs.djangoproject.com/en/dev/ref/contrib/databrowse/) That's it. Obviously if you don't want ALL the models to have this link, you should use a different recipe, for example this one: [http://www.djangosnippets.org/snippets/1016/](http://docs.djangoproject.com/en/dev/ref/contrib/databrowse/)

  • django
  • admin
  • databrowse
  • button
Read More

Language aware cache decorator

Caches a view based on the users language code, a cache_key and optional function arguments. The cache_key can be a string, a callable or None. If it's None, the the name of the decorated function is used. You can pass a tuple `func_args` of arguments. If passed, these arguments are part of the cache key. See examples for details.

  • cache
  • decorator
  • caching
Read More

convenience parent class for UserProfile model

This is just a reusable convenience parent class to allow you to create and administer an automatic user profile class using the following code: class UserProfile(UserProfileModel): likes_kittens = models.BooleanField(default=True) Whenever a `User` object is created, a corresponding `UserProfile` will also be created. That's it. NB: You will still need to set `AUTH_PROFILE_MODULE` in your settings :-) (PS: It would also be nice to have the resulting class proxy the `User` object's attributes like django's model inheritance does, while still automatically creating a `UserProfile` object when a `User` object is created :-)

  • userprofile
Read More

Quantize decimal template filter

Takes a float number (23.456) and uses the decimal.quantize to round it to a fixed exponent. This allows you to specify the exponent precision, along with the rounding method. And is perfect for monetary formatting taking into account precision.

  • filter
  • decimal
  • quantize
Read More

Decorator to modify reverse() to render SSL urls

This snippet monkey-patches Django's reverse() method (use for generating URLs from vew functions and parameters) to allow certain areas of your site to automatically have URLs with the correct SSL domain in place. This saves you from having to use unnecessary redirects to guide users to an SSL-encrypted version of a page. This should still be used alongside a redirect-based method (such as [this snippet](http://www.djangosnippets.org/snippets/85/)) to ensure that the user can't access an unencrypted version of the page Simply add the code to the files mentioned in the code. This obviously won't work anywhere that doesn't use reverse(), the admin app seems to be an example of this.

  • url
  • ssl
  • reverse
  • permalink
Read More

ImageMagick commands from Django Templates

A template filter which wraps imagemagick's `convert` command. The filter acts upon a source image path, and returns the filtered image path. usage: {{ source_path|convert:"-resize 64x64\!" }} The filter parameter is the processing arguments for an ImageMagick 'convert' command. See e.g. http://www.imagemagick.org/Usage/resize/ Every image created is saved in a cache folder. This code does not handle removing obsolete cached images. If the filtered image path exists already, no image processing is carried out, and the path is returned.

  • filter
  • image
  • templatetag
  • imagemagick
Read More

Query printer coroutine

If you would like to see the latest queries you have done when running a unittest, this is not so easy. You have to initialize the queries list and set DEBUG to True manually. Then you have to figure out a way to print the queries you want to see just now and format them. I.e., monitoring queries when doing TDD is a bit of a hassle, and this should help. This little helper does all this for you; your UnitTest only needs to import django.db.connection and store the current length (offset) of the queries list. Then, using Python's coroutine functionality, all you have to do is send that offset to the QueryPrinter coroutine (which you can, for example, initialize as a global variable or a class variable of your UnitTest) and you get the latest SQL printed as a simple table.

  • queries
  • unittest
  • debugging
  • printing
  • for
  • connection
Read More
Author: fnl
  • 0
  • 2

MultipleEmailsField

This is a custom field for multiple emails separated by comma. Original code was replaced by code from Django documentation: http://docs.djangoproject.com/en/dev/ref/forms/validation/ (MultiEmailField) so i'm not the author of the code, but just put it here to replace an outdated solution. Uses code from mksoft comment http://www.djangosnippets.org/snippets/1093/

  • multiple
  • forms
  • email
  • field
Read More

Django Drag and Drop Using YUI

I was trying to implement the django inline drag & drop, I cudn't understand. Then I have try with YUI library Copy the above code in the respective .py & download the YUI library and add the JS in your media folder. This will do..., Try this our , it's easy to use..,

  • django
  • yui
  • drag
  • drop
  • and
Read More

3109 snippets posted so far.