Login

All snippets

Snippet List

Fuzzy Time of Day

This filter will display the time as word(s) indicating roughly the time of day ("Morning", "Afternoon", "Evening", etc). For example, the following template snippet: Posted in the {{ post.date|fuzzy_time }} of {{ post.date|date:"F j, Y"} }}. will result in the following (assuming `post.date == datetime.datetime(2007, 6, 13, 20, 57, 55, 765000)`): Posted in the evening of June 13, 2007. The terms used and breakpoints (hours only) can be rather arbitrary so you may want to adjust them to your liking. See the docs for [bisect][] for help in understanding the code. Just remember you should have one less breakpoint than periods and the first breakpoint falls at the end of the first period. The idea was inspired by [Dunstan Orchard][1], although the code is *very* different (php case statement). He uses quite a bit more periods in a day, so you might want to take a look. [bisect]: http://docs.python.org/lib/module-bisect.html [1]: http://www.1976design.com/blog/archive/2004/07/23/redesign-time-presentation/

  • template
  • filter
  • time
Read More

manage.py with magic python path

I tend to have all my python code together in a 'python' directory. e.g. a typical project layout of mine looks like: /python /python/myproject - project python code /python/django - local copy of django /python/foolib - some third party library /media /templates ... Since I don't want to set the python path explicitly I just assume the 'manage.py' is in the right place and use its __file__ variable to set up the python path correctly. I use the same trick for my settings.py for setting MEDIA_ROOT and TEMPLATE_DIRS.

  • python
  • path
  • magic
  • pythonpath
  • sys.path
Read More

Make a string a fixed-width

This snippet is a template filter based on ["truncate letters"](http://www.djangosnippets.org/snippets/126/) only it will either truncate or pad a string to ensure the output is always a set width.

  • text
  • fixed-width
Read More

django image bundle template tag library

Django ImageBundle tag, an implementation of image bundle by google: http://code.google.com/p/google-web-toolkit/wiki/ImageBundleDesign. Here is how to use it: http://www.djangosnippets.org/snippets/278/

  • django
  • imagebundle
Read More

E-mail quoting filters and tags

Ttemplates and filters for quoting e-mails in templates. The `quoted_email` tag takes a template, renders it and quotes the result. The `quote_text` filter puts one or more levels of quotes around the passed text.

  • filter
  • tag
  • filters
  • email
  • tags
  • quoting
Read More

Humanize lists of strings in templates

A simple template filter for taking a list and humanizing it, converting: `["foo", "bar"]` to `"foo and bar"` `["foo", "bar", "baz"]` to `"foo, bar and baz"` `["foo", "bar", "baz", "42"]` to `"foo, bar, baz and 42"`

  • filter
  • lists
  • filters
  • humanize
Read More

HTTP headers view decorator

Decorator adding arbitrary HTTP headers to the response. This decorator adds HTTP headers specified in the argument (map), to the HTTPResponse returned by the function being decorated. Example: @headers({'Refresh': '10', 'X-Bender': 'Bite my shiny, metal ass!'}) def index(request): ....

  • http
  • view
  • decorator
  • headers
Read More

QLeftOuterJoins

This hack replaces all INNER JOINs inside to the LEFT OUTER JOINs (see http://code.djangoproject.com/ticket/3592 for explanation). Use: QLeftOuterJoins(Q(...) | Q(...) & (Q(...) | ....)).

  • q
  • query
  • join
  • left
  • outer
Read More

QEmpty

Q() value in Django is identity for & operation: Q() & foo & bar & ... == foo & bar & ... QEmpty() is complimentary identity for | operation: QEmpty() | foo | bar | ... == foo | bar | ... QEmpty() itself returns empty queryset. Handy for complex query generation.

  • q
  • query
Read More

Function/Stored Procedure Manager

Ever want to call stored procedures from Django easily? How about PostgreSQL functions? That's that this manager attempts to help you with. To use, just stick this in some module and in a model do: class Article(models.Model): objects = ProcedureManager() Now you can call procedures (MySQL or PostgreSQL only) to filter for models like: Article.objects.filter_by_procedure('ProcName', request.user) This will attempt to construct a queryset of objects. To just get random values, do: Article.objects.values_from_procedure('ProcName', request.user) Which will give you a list of dictionaries.

  • function
  • db
  • manager
  • db-api
  • stored-procedure
Read More

locale based on domain

This is something we're using over at Curse to keep things clean and simple for our users. * We needed a url for any language code (which the domain provides) vs a cookie * We needed a to only store 2 letter codes in the db for each language (thus the key doesn't always match the code) This consists of two major modifications: * LANGUAGES in settings.py is a completely different format and would need changed based on your setup * the locale middleware has a couple absolute instances of where to point a user by default.

  • middleware
  • i18n
Read More

script for run a django task

when running a console script to access django or your models, you need to setup django environment properly and that is very annoying if you have quite a few scripts. this script can be used as a wrapper just like the manage.py. put it in your project root and type this: python run.py myproj.appname.filename.functionname [arguments] and define your function as def functionname(argv[]): ...

  • task
  • console
  • script
Read More

jQuery Autocomplete

A merged version of the many jQuery autocomplete widgets. See [1](http://php.scripts.psu.edu/rja171/widgets/autocomplete.php) and [2](http://www.dyve.net/jquery/?autocomplete) for more information.

  • jquery
  • autocomplete
Read More

3110 snippets posted so far.