Login

All snippets written in Python

Snippet List

Skip only specified spaces

Similar to `{% spaceless %}`, but only removes spaces followed by a special marker (`&nosp;`). Helps keeping proper indentation: {% skipspaces %} Lots of coffee {% if from_brazil %} from Brazil {% else %} from Columbia {% endif %} {% if tea %}&nosp;, some tea{% endif %} and a cake. {% endskipspaces %} Otherwise you'd have to write it in one looong line or get used to living with spaces before commas :) NB the marker itself is stripped from rendered html as well.

  • spaceless
  • whitespace
  • skip
  • spaces
Read More

Coalesce for F()

Updating fields that allow for NULLs must take care about those NULLs. Use case: `Message.objects.filter(id=1).update(price=CF('price', 0) + 7)` `Message.objects.filter(id=1).update(status=CF('status') // 's')` Works for postgres. Supposedly works for mysql. Didn't test with other backends.

  • coalesce
Read More

Async PIL resize of images

Call resize_image to replace the image with a resized and normalized version of itself. I recommend doing this with celery, but you could also hook it up to the admin interface if you're not impatient.

  • Images
  • PIL
Read More

Pretty Paginator Tag

This is a modified version of the (http://djangosnippets.org/snippets/73/) paginator snippet. It works with the django paginator.

  • paginator
  • 'digg style'
Read More

autotranslate po files using microsoft translator

This snippet is inspired by [dnordberg](http://djangosnippets.org/snippets/1048/) 's translation script which used google's translation script. But after google translation is no more free. I put together it to use microsoft translator's python wrapper by [openlab](https://github.com/openlabs/Microsoft-Translator-Python-API) usage ----- 1. get a bing appID from [here](https://ssl.bing.com/webmaster/developers/appids.aspx) and replace it on the top of script 2. sudo apt-get install translate-toolkit 3. sudo pip install microsofttranslator 4. sudo autotranslate.py [pofile] [sourcelangcode] [targetlangcode] Enjoy!!! **Prabhat Kumar Gupta** Python/Django Dev [http://www.prabhatgupta.com](http://www.prabhatgupta.com)

  • internationalization
  • i18n
  • translation
  • po
Read More

SASS/SCSS include template tag.

You'll need to `pip install pyScss` first. Converts on the fly, so you won't want to use this for much more than just testing. Usage in a template: {% load sass %} {% include_sass "disclosures/css/base.scss" %} {% include_sass "disclosures/css/grid.scss" %}

  • template
  • tag
  • tags
  • scss
  • sass
Read More

Create permissions for proxy models

Until [#11154] is fixed, django won't create permissions for proxy models for you as it does with other models, even the one you define using the [`permissions` option](https://docs.djangoproject.com/en/dev/ref/models/options/#django.db.models.Options.permissions) on your models. This snippet will make sure all permissions are create for proxy models. Just make sure this code gets loaded after the `update_contenttypes` handler is registered for the `post_syncdb` signal. Putting this code in one of your `INSTALLED_APPS' after `django.contrib.contenttypes' should do it.

  • admin
  • permission
  • proxy
Read More

True Unique Boolean Decorator

Useful when you want to keep only one instance of a model to be the default one. This is a decorative way of doing the same as in http://djangosnippets.org/snippets/1830/ Can this be made better as a class decorator (not having to declare explicitly the save method) ?

  • model
  • unique
  • boolean
Read More

Improved Button Admin

An improved version of http://djangosnippets.org/snippets/1016/ which lets you add separate buttons for change_list and change_form pages in the admin.

  • admin
  • buttons
Read More

Clean Reversion History: Remove unimportant Changes

If you use this pattern to track changes in the auth user table: from django.contrib.auth.models import User from reversion.helpers import patch_admin patch_admin(User) you can't see important changes, since a version is created for every login if a user. If you want to get rid of changes which only change unimportant stuff you can use this middleware. This middleware can be used for every model. Just use this pattern in your settings: REVERSION_CLEAN={ 'django.contrib.auth': {'User': ['last_login']}, }

  • reversion
Read More

Dump a model instance and related objects as a Python data structure

This utility makes a text dump of a model instance, including objects related by a forward or reverse foreign key. The result is a hierarchical data structure where * each instance is represented as a list of fields, * each field as a (<name>, <value>) tuple, * each <value> as a primitive type, a related object (as a list of fields), or a list of related objects. See the docstring for examples. We used this to make text dumps of parts of the database before and after running a batch job. The format was more useful than stock `dumpdata` output since all related data is included with each object. These dumps lend themselves particularly well for comparison with a visual diff tool like [Meld](http://meldmerge.org/).

  • serialize
  • dump
  • model
  • instance
Read More

django piston use origin django auth

Django-piston have two build-in authentication handlers, the HttpBasicAuthentication and OAuthAuthentication. This snippet give another choice which use the django auth. It can support ajax and normal request.

  • django
  • auth
  • piston
Read More

2955 snippets posted so far.