Login

All snippets

Snippet List

UTC-based astimezone filter

A version of http://djangosnippets.org/snippets/2388/ which assumes UTC-based dates. This is when you store dates in UTC (as you should), and want to display them in your site's local timezone, and you notice that Django's timesince/time template tags still do not support timezones.

  • timezone
  • utc
  • timezones
  • times
Read More

Property Attributes in Memcache

Memcached Property Attributes ----------------------------- Setting the attribute will store the value in memcached; accessing the attribute will retrieve from memcached. Most useful as a way of simulating memcached "fields" on model instances. See the docstring for details.

  • cache
  • memcached
  • descriptors
Read More
Author: ori
  • 0
  • 0

ClearableFileInput with image preview

This widget allows you to display preview images with adjustable width and length of the link: [example](http://img526.imageshack.us/img526/6588/screenshotat20111026215.png) AdvancedFileInput(preview=True, image_width=200) For other files, you can adjust the length of the link without preview: [example](http://img845.imageshack.us/img845/6588/screenshotat20111026215.png) AdvancedFileInput(preview=False, url_length=30) by default, parameters are: preview = True url_length = 30 image_width = 200

  • imagefield
  • preview
  • imagepreview
  • previews-on-imagefield
Read More

Disable ordering in the admin for a model

Django admin orders models by their primary key by default, which can be undesirable on very large tables. This shows how to disable any ordering on a model. Note that this behavior is fixed in 1.4 trunk.

  • admin
  • ordering
Read More

Remove old fields on dumpdata generated json

A small script that takes a manage.py dumpdata generated json file, and removes fields of the specified models. I needed this because i kept my initial data on a json file and after I removed a field on one of my models, the script wouldn't work anymore.

  • json
  • loaddata
  • fixture
  • dumpdata
  • migrate
  • removed field
Read More

Get Client IP Behind Proxy

If your application server is behind a proxy, `request.META["REMOTE_ADDR"]` will likely return the proxy server's IP, not the client's IP. The proxy server will usually provide the client's IP in the `HTTP_X_FORWARDED_FOR` header. This util function checks both headers. I use it behind Amazon's Elastic Load Balancer (ELB).

  • request
  • ip
Read More

Persistent Session Debugging with Django Debug Toolbar

When using [django debug toolbar](https://github.com/django-debug-toolbar/django-debug-toolbar), I like to be able to turn debugging on and off without having to edit my settings file. This callback makes that possible. Add `?debug=on` to the URL to turn debugging on. It will remain on in the current session until you turn it off with `?debug=off`. Make sure your session middleware comes before your debug toolbar middleware.

  • session
  • debug
  • debug-toolbar
Read More

Custom CSS class in Form with template tag filter

It was based in: http://djangosnippets.org/snippets/1586/ Instead of doing this: 'attribute_name = forms.CharField(widget=forms.TextInput(attrs={'class':'special'}))` You can do this in your template: {{ form|cssclass:"attribute_name:special_class"|cssclass:"other_attribute:special_class" }}

  • filter
  • templatetag
  • css
  • form
  • class
Read More

Haystack objects in one query

Actually the best way to handle this is to use built-in http://docs.haystacksearch.org/dev/searchqueryset_api.html#load-all I just missed it while checking documentation and wrote this crappy snippet!

  • django
  • haystack
  • SearchQuerySet
Read More

Filter by taggit tags in the admin

A FilterSpec that can be used to filter by taggit tags in the admin. To use, simply import this module (for example in `models.py`), and add the name of your TaggableManager field in the list_filter attribute of your ModelAdmin class.

  • filter
  • admin
  • taggit
Read More

ModelForm Class saving m2m

**Your model:** class TicketItem(models.Model): hours = models.DecimalField(decimal_places=2, max_digits=6) day = models.DateField() order = models.ForeignKey(Order) tickets = models.ManyToManyField(Ticket) Now you want to auto save m2m fields in your forms.TicketItemCreateForm: 1. inherit from m2mForm-Class 2. define m2m_field(s) **Example:** class TicketItemCreateForm(m2mForm): m2m_field = 'tickets' class Meta: model = models.TicketItem

  • forms
  • m2m
  • class
  • modelform
Read More

alientag

Renders enclosing contents as it is. Useful to avoid tags conflict with certain javascript libraries (eg. jquery-tmpl.js, mustache.js) Example usage: `<script class="content-tmpl" type="text/x-jquery-tmpl">` `{% alien %}` `{{ tmpl(results) "#results .item-tmpl" }}` `{% endalien %}` `</script>`

  • templatetag
Read More

3110 snippets posted so far.