Login

Most bookmarked snippets

Snippet List

Breadcrumbs filter

Filter for a list if breadcrumbs. All necessary code included but obviously only the breadcrumbs method that is needed with necessary changes for esc(). I use it as {% load breadcrumbs %} {{ request.path|breadcrumbs:"" }} enclosed in an unordered html list with id="breadcrumbs"

  • filter
  • breadcrumbs
Read More

Load Windows ICO files

PIL IcoImagePlugin is twelve year old and it can't handle recent Windows ICO files. Here is a function that handles all ICO versions and preserve transparency. Usage: # Load biggest icon from file image = load_icon('icon.ico') # Save third icon as PNG load_icon('icon.ico', 2).save('icon.png')

  • image
  • pil
  • ico
Read More
Author: dc
  • 2
  • 2

Dynamic growing model

This model is designed for my webshop. The Client-object is very 'stretch-able' by defining more fields to the client. These extra fields ares stored in the ClientConfig-object. Be sure to create a new Client-instance first and SAVE it! Without a client.id the ClientConfig won't work.

  • django
  • dynamic
  • model
  • example
  • client
Read More

simple app using RestViews

Inspired by [Eric Florenzano's](http://www.eflorenzano.com/) post about [writing simple and very fast pure-WSGI applications](http://www.eflorenzano.com/blog/post/writing-blazing-fast-infinitely-scalable-pure-wsgi/). Using [RestView](http://www.djangosnippets.org/snippets/1071/) approach. More about it on [my blog](http://my.opera.com/kubiku/blog/2009/01/16/bare-wsgi-vs-python-frameworks-django-chapter)

  • rest
Read More

Lazy options on ModelForm fields - like setting a ModelChoiceField queryset from the view

Example view code: lazy_field_options = { 'field_name_that_is_m2m': { 'queryset': YourRelatedModel.objects.filter(groups=request.user.groups.all()), }, 'field_name_that_is_fk': { 'queryset': YourOtherRelatedModel.objects.filter(slug=request_slug), }, } modelform = YourModelForm(jpic_field_options=lazy_field_options) # after the modelform has called for parent __init__, it will set # options for each field if possible.

  • hack
  • form
  • queryset
  • modelchoicefield
  • modelform
  • modelmultiplechoicefield
Read More

Boolean Image Flag TemplateTag

Something I end up doing all the time, making a boolean variable show up as a nice image. With this code you can do the following: `{% boolean_img user.is_active %}` And get the following output: `<img src="/media/icons/accept.png" alt="True" />` All you need to do is use the custom templatetag code, load it in your template and use the `boolean_img` tag. **Adjust templates, html and images where needed**

  • image
  • templatetag
  • boolean
  • flag
Read More

(Modified/Improved) MultiQuerySet

My modified version of the [MultiQuerySet by mattdw](http://www.djangosnippets.org/snippets/1103/) (see the link for further information). My purpose for this was to enable me to combine multiple different types of querysets together, which could then be iterated on as one object (i.e. like a tumblelog).

  • multiple
  • queryset
  • chain
Read More

BigIntegerField and BigAutoField

Allows to create bigint (mysql), bigserial (psql), or NUMBER(19) (oracle) fields which have auto-increment set by using the AutoField of django, therefore ensuring that the ID gets updated in the instance when calling its 'save()' method. If you would only subclass IntegerField to BigIntegerField and use that as your primary key, your model instance you create would not get the id attribute set when calling 'save()', buy instead you would have to query and load the instance from the DB again to get the ID.

  • "primary
  • key"
  • bigserial
  • bigint
  • auto_increment
Read More
Author: fnl
  • 1
  • 2

Cycling MEDIA_URL context processor

This is a context processor that will allow you to cycle the values of your MEDIA_URL context variable. It will cycle through the urls defined in settings.MEDIA_URLS, so that you can distribute your media url's between multiple servers.

  • cycle
  • media_url
Read More

static tag

Django template tag for easy static files linking into your html. See docstring for documentation.

  • static
Read More

Preferred Domain decorator function.

From time to time I often have to work with a site that has two domain names. This can be an issue when dealing with mapping api keys so to solve this problem I whipped up a decorator function that allows a preferred domain to be enforced on a per view basis. It wouldn't be too much to take this onto a middleware, but for my usage I just wanted a per view granularity and a decorator function suited my needs quite nicely.

  • decorator
  • domain
  • preferred-domain
Read More

Safer cache key generation

If any cache keys you generate include user (staff or public) supplied data, they may: be too long, contain invalid characters (at least by memcache's standards), or both. This helper will sub out any invalid characters and md5 the key if it's too long. Additionally, it'll insert the CACHE_MIDDLEWARE_KEY_PREFIX from django.conf.settings for you. In your memcache instances are used by multiple applications (Django or otherwise) this can help ensure your keys are unique to that a particular app on a particular site.

  • memcache
  • cache
  • caching
Read More

Basic CouchDB Paginator (Updated)

This is a very basic paginator for CouchDB ViewResults. It's usage is similar to the django one. **CouchPaginator** is almost like the django version with one exception: It's constructor needs an additional argument `pages_view` which is either a reduce result or an integer which holds the total number of objects across all pages. *Example Map/Reduce for pages*: > map = function(doc) { > if(doc.type=="application" || doc.type==\"inquiry\"){ > emit([doc.meta.user.toLowerCase(), doc.type], 1); > } > reduce = function (doc, value, rereduce){ > return sum(value); > } **SimpleCouchPaginator** is much simpler and needs no total number and can only paginate by "next" and "previous" page. Use this if you don't have an reduce view which tells you the amount of total object. An Example setup of CouchDB with django can be found at: [Eric Florenzanos post about django+couchdb](http://www.eflorenzano.com/blog/post/using-couchdb-django/) [See example usage](http://www.djangosnippets.org/snippets/1209/)

  • pagination
  • paginator
  • couchdb
Read More

Add site info to request context

Sometimes you want to generate a **really** absolute URL, but the built-in url tag only generates a URL relative to the current domain. This context processor adds the extra information needed to the request context, so you can generate an absolute URL in a template like so: `{{ protocol }}://{{ domain }}{% url someview %}` This is similar to how the password reset email from contrib.auth generates the full URL in the email. Save this somewhere as context_processors.py (or add to existing file if you have one), and add context_processors.site to your TEMPLATE_CONTEXT_PROCESSORS setting.

  • contextprocessor
  • domain
  • site
Read More

3110 snippets posted so far.