Login

All snippets

Snippet List

Gmail date format

Short and sweet date format from Gmail. Use as {{ message.sent_at|humantime }} Works for future dates too.

  • date
  • format
  • gmail
  • google mail
  • dateformat
Read More

GeoJSON Serializer for GeoDjango (gis)

**Please use the updated version http://djangosnippets.org/snippets/2596/** Unfortunately the built in Django JSON serialzer encodes GeoDjango GeometyrField as simple text. This snippet extends django's serializer and adds support for GeoJson format. Built in JSON serializer output: [{"pk": 1, ... "geopoint": "POINT (-76.5060419999999937 44.2337040000000030)" ... }] GeoJSON serializer ouput: [{"pk": 1, ... "geopoint": {"type": "Point", "coordinates": [-76.503296000000006, 44.230956999999997]}}]

  • GeoJson
  • Serializer
Read More

template filter to check login status

In templates sometimes you need to display some menu by checking whether the user is logged in or not. So use the above filter as shown below {% with request|check_login as logout %} {% if logout%} display something.... {% endif %} {% endwith %}

  • template
  • filters
  • custom
Read More

Pygmentify a code using template filter

The above snippet can be added as a custom filter for rendering code snippets in django templates. A simple '|render' next to any source code will do. To add css, use the following command to generate css file. pygmentize -S emacs -f html > pygments-colorful.css And link this css file to the template.

  • template
  • django
  • pygments
  • custom-filters
Read More

FirstRun Middleware

Simple piece of middleware that redirects all requests to **settings.FIRSTRUN_APP_PATH**, until a lockfile is created. Tested on 1.3 only, but I do not believe it requires any special functionality beyond that provided in 1.1 This is useful if you need to force a user to run an installer, or do some configuration before your project can function fully. At first glance, such a thing would generate a lot of hits on the disk. However, once the lockfile has been created, the middleware unloads itself, so when a project is in a production environment, the lockfile is only checked once per process invocation (with passenger or mod_wsgi, that's not very often at all). Once your user has completed your FirstRun app, simply create the lockfile and the project will function as normal. For it to function, the following settings must be configured: * **settings.PROJECT_PATH** - absolute path to project on disk (e.g. */var/www/project/*) * **settings.FIRSTRUN_LOCKFILE** - relative path of the lockfile (e.g. */.lockfile*) * **settings.FIRSTRUN_APP_ROOT** - relative URL of the App you want to FirstRun (eg.*/firstrun/*)

  • middleware
  • django
  • redirect
Read More

Add GET parameters from current request

The tag generates a parameter string in form '?param1=val1&param2=val2'. The parameter list is generated by taking all parameters from current request.GET and optionally overriding them by providing parameters to the tag. This is a cleaned up version of http://djangosnippets.org/snippets/2105/. It solves a couple of issues, namely: * parameters are optional * parameters can have values from request, e.g. request.GET.foo * native parsing methods are used for better compatibility and readability * shorter tag name Usage: place this code in your appdir/templatetags/add_get_parameter.py In template: {% load add_get_parameter %} <a href="{% add_get param1='const' param2=variable_in_context %}"> Link with modified params </a> It's required that you have 'django.core.context_processors.request' in TEMPLATE_CONTEXT_PROCESSORS

  • get
  • request
  • parameters
  • add
Read More

ad-hoc request authentication

This is auth_data_required decorator analogous to login_required where authentication data must come in POST of request. It's useful for API. I took the idea from [Tumblr API](http://www.tumblr.com/docs/en/api).

  • authentication
Read More

Generic views with row-level permission handling

These generic views extend default views so that they also do permission checking on per-object basis. * detail, update and delete - check access for user * create - create permissions for user on object * list - narrow object list with permissions Classes prefixed with Owned are example implementation where user has access to object if designed object attribute references him. Example: `create_article = OwnedCreateView.as_view(owner='creator', model=Article, form_class=ArticleForm, success_url='/articles/article/%(id)d')`

  • generic-views
  • permissions
Read More

Field List Tag

Tag for iterating through the fields of an object from a template. The tag is passed two string arguments, the name of the model to use (which is then looked up in the context dictionary), and the format string. The format string should include two string place holders. The tag will output each field name and value according to the format string supplied.

  • properties
  • field
  • iterate
Read More

math tag

Syntax: {% math <argument, ..> "expression" as var_name %} Evaluates a math expression in the current context and saves the value into a variable with the given name. "$<number>" is a placeholder in the math expression. It will be replaced by the value of the argument at index <number> - 1. Arguments are static values or variables immediately after 'math' tag and before the expression (the third last token). Example usage, {% math a b "min($1, $2)" as result %} {% math a|length b|length 3 "($1 + $2) % $3" as result %}

  • math
  • min
  • max
Read More

Other translation block

Use in a with statement to set the translation to another locale for a block >>> from django.utils.translation import ugettext >>> ugettext('title') u'title' >>> with Translation('fr') as locale: ...: print locale.locale ...: print ugettext('title') ...: ...: fr titre >>> ugettext('title') u'title'

  • i18n
  • mail
  • translation
Read More

Simple DRY Tabs using Django 1.3

I was just playing around with Django 1.3 and came across the new 'with' addition to the include tag. I thought this was a super elegant way to implement a DRY tab menu without having to move menu code into the views. Enjoy!

  • menu
  • dry
  • tabs
  • menus
  • tabbed
  • tab
Read More

CollectionFrom (GeometryCollection <-> Geometry Fields!)

Motivation: We can't use GeometryCollections to do filters, etc in GeoDjango due to incomplete underlying libraries. But with this CollectionFrom field we can get all the benefits of working with GeometryCollections but still query based on points, lines, polys. If you're using GeometryCollectionFields and see this error: DatabaseError: Relate Operation called with a LWGEOMCOLLECTION type. This is unsupported. Then this will probably be helpful for you.

  • geodjango
  • geo
  • geometrycollection
Read More

3110 snippets posted so far.