Login

All snippets

Snippet List

TextField

Alex Gaynor presented this idiom at EuroDjangoCon 2009.

  • forms
  • textfield
Read More

make templates fail loudly in dev

Add the line shown, or something similar, to your settings/dev.py, so that you can more clearly see when django is silently hiding errors in your template tags.

  • template
  • debugging
Read More

inspectdb fixer

This snippet parses the output file of inspectdb and does some alterations. Mostly useful for people who regenerates models from constantly changing legacy databases. The snippet will: *Add quotes around foreign key classes, so the ordering is not significant *Append a related_name property to each foreign key with the value model class name + db_column name to evade collisions in reverse queries like: example.model: Reverse query name for field 'foreignkey' clashes with related field 'model2.foreignkey'. Add a related_name argument to the definition for 'foreignkey'. There's a slight performance degradation with using quotes class name instead of passing the class though.

  • models
  • inspectdb
  • database-import
Read More

Generic Permissions

A mixin to define permissions on models. This is more of an abstract model to subclass/customise than a plug-in solution. Explanations are [here](http://www.muhuk.com/2009/05/django-permission-system/).

  • models
  • model
  • permission
  • authorization
Read More

Yet another list partitioning filter

A simple template filter for breaking a list into sublists of a given length, you might use this on an ecommerce product grid where you want an arbitrary number of rows of fixed columns. Unlike the other partitioning filters I've seen, this doesn't try to distribute the rows evenly, instead it fills each row for moving onto the next. This filter preserves the ordering of the input list.

  • template
  • filter
  • partition
Read More

testshell

This commands runs a Python interactive interpreter with test database and data from the given fixture(s). It is usable if you want to play with test database. See also testserver docs

  • fixtures
  • shell
  • testshell
Read More

UUIDField oriented django User

This code monkey-patches the default User model to rather use a primary key of UUIDField (see http://www.djangosnippets.org/snippets/1496/). This code also makes the email field required. This code is wildly dangerous, not completely future proof, and probably not advisable to use. If you do wish to use it, it will be easiest to implement on a fresh db with a syncdb. If you need to migrate existing user data the onus is on you to figure out an appropriate db migration plan. I placed this code in a models.py, but it is probably more reliably placed in urls.py

  • user
  • auth
  • uuid
  • monkeypatch
Read More

UUIDField

This snippet provides a uuid field for models, improving on the work in http://www.djangosnippets.org/snippets/335/

  • model
  • field
  • uuid
Read More

CompressedTextField for Django 1.0+

This snippet updates http://www.djangosnippets.org/snippets/383/ for Django 1.0+, and adds support for sqlite3. Original snippet text: A CompressedTextField to transparently save data gzipped in the database and uncompress at retrieval.

  • text
  • model
  • field
  • compressed
  • gzip
Read More

Generate QR Code image for a string

Generate QR Code image from a string with the Google charts API http://code.google.com/intl/fr-FR/apis/chart/types.html#qrcodes Exemple usage in a template {{ my_string|qrcode:"my alt" }} will return the image tag with * src: http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=my_string&choe=UTF-8 * alt: my alt"

  • filter
  • template-filter
  • filters
  • template-filters
  • qr-code
  • qr-codes
Read More

Scalable and invalidateble cache_page decorator

This cache_page decorator can be used in replacement to Django's django.views.decorators.cache.cache_page. It resolves two problems: - invalidation (its cache key is not dependent of header nor request, then you can use model signals (or method 'put' in Google App Engine) to invalidate a URL or a list of them) - easier to scale (can be used at once memcached server by many differente servers) Feel free to show me where it can have problems or limitations. **Updated and improved a lot**

  • cache
  • decorator
  • invalidation
Read More

Collapsed stacked inlines

A simple jQuery javascript that collapses all stacked inline rows for better handling of large inline fieldsets. It also adds "Show"/"Hide"-buttons for showing/hiding each row, which could be customized and styled using css. **Usage (see below for example):** Include the javascript on your admin page, together with jQuery, and it'll automatically affect all stacked inlines. **Developed for:** * jQuery 1.3.2 * Django trunk (tested in Django v1.0.2) * (Might work with other versions with or without adjustments, but not tested) **Use example: ** *admin.py:* class DateInline(admin.StackedInline): model = Date extra = 10 class EventAdmin(admin.ModelAdmin): inlines = [DateInline] class Media: js = ['js/jquery-1.3.2.min.js', 'js/collapsed_stacked_inlines.js',] admin.site.register(Event, EventAdmin)

  • javascript
  • admin
  • jquery
  • inline
  • collapse
  • stacked
Read More

3109 snippets posted so far.