Login

All snippets written in Python

Snippet List

Image Preview on ImageField in admin

This makes a 100x100 thumbnail of the image for the image field and shows it in the admin form combination of http://djangosnippets.org/snippets/955/ and http://www.psychicorigami.com/2009/06/20/django-simple-admin-imagefield-thumbnail/

  • admin
  • widgets
  • widget
  • image-previews
  • previews-on-ImageField
Read More

django_production.wsgi

The above conf file the easiest way to point the env and the settings together , the user has to point the django.wsgi file with the correct path and it should work fine and one can directly run it in apache

  • django
  • wsgi
Read More

Default URL handler

Default URL handler allows views to be loaded without defining them in the urls.py. Views will therefore be loaded based on the pattern of the browser url. For example http://host/app_name/view_name will load project_name.app_name.views.view_name. Though I would not used this in production, it can be used to speed-up development.

  • url
Read More

Admin: return to change_list with filter and pagination applied

By default every time you change and save an object in the admin, the change_list "jumps" to the first page, so filters you used to find the object (or the pagination-page) have to be applied again. If you have to go through a multi-object-list step-by-step this could become really annoying. The above snippet changes this behaviour by returning to the referring URL when saving. Included in this URL are variables for the filters/pagination. The snippet is part of your custom Model.admin in admin.py.

  • filter
  • admin
  • pagination
  • change_list
Read More
Author: fx
  • 4
  • 8

Simple template tag to do |stringformat filter with format from a variable

Formats a django variable using a python string formatting as specified in another template variable. Similar to |stringformat. Takes two arguments: the django template variable with the item to be formatted and the django template variable containing the format string. {% pyformat number formatstringvar %} Place this file in `appname/templatetags/pyformat.py` and you're good.

  • template-tag
  • string-formatting
Read More

Yet another query string template tag

This one works works with or without query string dicts defined in the context. And it handles replacement, addition and removal of values for parameters with multiple values. Usage: {% url view %}{% query_string qs tag+tags month=m %} where `view`, `qs` (dict), `tags` (list of strings) and `m` (number) are defined in the context. Full detail in the doc string.

  • url
  • template-tag
  • query-string
Read More

JSON Decimal encoder

Python json module or simplejson don't support Decimals out of the box. Here's an encoder that casts Decimals into floats and then displays them in desired format.

  • json
  • decimal
  • encoder
Read More

(Almost) Single table polymorphism in Django

An emulation of "table per hierarchy" a.k.a. "single table inheritance" in Django. The base class must hold all the fields. It's subclasses are not allowed to contain any additional fields and optimally they should be proxies. They however may provide additional methods to operate on the declared field. The presented solution supports implicit inheritance, even across ForeignKeys, and ManyToManyFields. No additional database hits are imposed.

  • django
  • django-models
  • table-per-hierarchy
  • single-table-polymorphism
  • polymorphism
Read More

Plaintext format (advanced spaceless)

A template loader useful for writing templates with carefully controlled newlines and spaces while retaining readable template source code formatting. Start the template with PTFTAG (`{#ptfable#}`, here) to allow it to be processed. Common problems with doing it to most templates as-is is use of newlines to separate words and multiple spaces between tags where spaces are still needed (which is problematic with `spaceless` tag as well). Currently intended as a template loader wrapper, and is suggested to be used with cached loader. Example settings.py configuration: _lp = lambda lo, *ar: (lo, ar,) # loader, arguments TEMPLATE_LOADERS = ( _lp('django.template.loaders.cached.Loader', # cache _lp('ptf.template.ptftemplateloader.Loader', # ptf 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', #'django.template.loaders.eggs.load_template_source' ), # ptf ), # cache ) (change `ptf.` to wherever in python path you've put it). You might also need couple of simple template tags for explicitly inserting newlines or whitespaces: def br(): return "\n" br = register.simple_tag(br) # XHTML-XMPP-template compatible. def brx(): return "<br />\n" brx = register.simple_tag(br) def ws(): return " " ws = register.simple_tag(ws) .

  • spaceless
  • ptf
Read More

Debug middleware for displaying sql queries and template loading info when ?debug=true

Originally based on: [http://djangosnippets.org/snippets/1872/](http://djangosnippets.org/snippets/1872/) The way the original snippet formatted sql didn't work for mysql properly so I taught it to use the sqlparse python module. Now it looks like this when settings.DEBUG=True: SQL executed: SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."session_key" = d326108d313a2e5c5fb417364b005ab9 AND "django_session"."expire_date" > 2011-04-08 14:54:13.969881) took 0.001 seconds SELECT "auth_user"."id", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."password", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."is_superuser", "auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = 2 took 0.000 seconds Additionally, this middlware is enabled conditionally based upon the url query string "debug". You can enable it for a single request by appending: ?debug=true to the url.

  • middleware
  • development
  • debug
Read More

Smart Spaceless

This 'smart_spaceless' template tag is a replacement for Django's built-in 'spaceless'. If settings.DEBUG = True, spaces will not be removed to make debugging your template code easier. When DEBUG = False, spaces will be removed. Happy coding!

  • template
  • spaceless
  • template tag
  • remove spaces
  • template tags
Read More

Generic Autodiscovery

Admin-like autodiscover for your apps. I have copy/pasted this code too many times...Dynamically autodiscover a particular module_name in a django project's INSTALLED_APPS directories, a-la django admin's autodiscover() method.

  • autodiscover
Read More

2955 snippets posted so far.