Login

Top-rated snippets

Snippet List

isoutc template filter

Use this template filter to produce an ISO format UTC datetime string from a [timezone aware](http://docs.python.org/library/datetime.html#datetime.tzinfo) [datetime](http://docs.python.org/library/datetime.html#datetime.datetime) object. Usage example in a template: `<input name="when" type="hidden" value="{{ dt|isoutc }}"`. You must have [dateutil](http://labix.org/python-dateutil) installed for `tz.tzutc()` to work. And of course, you'll need to load it as a [custom tag](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags) to use it. The output format differs from python's [datetime.isoformat](http://docs.python.org/library/datetime.html#datetime.datetime.isoformat) by ignoring the `microsecond` and including a "Z" suffix for the UTC timezone. For ease of use, it is also not double quoted as the standard suggests.

  • datetime
  • utc
Read More

FuzzyDateTimeField

FuzzyDateTimeField is a drop in replacement for the standard [DateTimeField](http://docs.djangoproject.com/en/dev/ref/forms/fields/#datetimefield) that uses [dateutil.parser](http://labix.org/python-dateutil#head-a23e8ae0a661d77b89dfb3476f85b26f0b30349c) to clean the value. It has an extra keyword argument `fuzzy=True`, which allows it to be more liberal with the input formats it accepts. Set `fuzzy=False` for more strict validation.

  • datetime
  • humanize
Read More

Letterbox for sorl-thumbnail

This custom processor is meant for use with sorl-thumbnail to add letterboxing functionality. Add to your THUMBNAIL_PROCESSORS like so: `THUMBNAIL_PROCESSORS = ( 'sorl.thumbnail.processors.colorspace', 'sorl.thumbnail.processors.autocrop', 'sorl.thumbnail.processors.scale_and_crop', 'sorl.thumbnail.processors.filters', # custom processors 'utils.processors.ltbx', ) ` and then use in your templates like so: ` {% thumbnail model.img_field 200x150 ltbx as thumb %} <img src="{{ thumb }}" width="{{ thumb.width }}" height="{{ thumb.height }}" />` Enjoy.

  • image
  • processor
  • sorl
Read More

soaplib service integration 2

Based on our [first version of soaplib service integration](http://www.djangosnippets.org/snippets/979/), this second one adds Basic Auth with credentials specified in settings. It can be tested with [django soaplib test cliente](http://www.djangosnippets.org/snippets/1406/)

  • soap
  • soaplib
  • wsdl
Read More

django soaplib test client

This code *monkey patches* soaplib client to allow the usage of django test client for local web service testing (without a running server). It adds *basic* authentication.

  • soap
  • soaplib
  • test-client
Read More

Overwrite some views in settings.py

If you app defines some URLs with a name, and you want to overwrite this at project level with a different view you can use this snippet. You only need to change on line in the application code (the import statement).

  • urls
  • settings
  • reverse
Read More

Filter to adjust forloop.counter across pages in a paginated view

**Update:** Never mind. See [dc's comment](http://www.djangosnippets.org/snippets/1391/#c1763) below for a much easier way to do this. I recently had to write a template for a paginated view which displayed a serial number for each `object` in the `object_list`. I normally use `forloop.counter` for general purpose serial numbers. However this did not work with paginated views as the counter gets reset in each page. This caused the serial numbers to go from 1 to #-of-results-in-the-page and then repeat. **Assumptions:** The `adjust_for_pagination` filter adjusts the value of `forloop.counter` based on the current page. `Page` and `is_paginated` variables are expected to be present in the context. These should respectively denote the current page number (1 based) and if the results are paginated. `RESULTS_PER_PAGE` is currently taken from the settings file. I couldn't think of a way to pass this value also from the template.

  • filters
  • pagination
  • forloop.counter
Read More

request_logger

Simple logger, stores all query parameter and post parameters for each query.

  • debugging
  • logger
Read More

function tracing decorator

This is a decorator that logs function arguments, return object and time taken for execution as well as any exception that might have been raised by the function. Useful for debug logging.

  • decorator
  • debugging
  • tracing
Read More

iPhone Redirect Middleware

I didn't really like the current state of iPhone/Mobile redirect middleware mainly because I wanted something that was closer to twitters use case. So I came up with this. I don't think it a great snippet and I will probably fix it in the near future. But it works.

  • middleware
  • session
  • redirect
  • iphone
Read More

3110 snippets posted so far.