Login

All snippets

Snippet List

Active link

I needed a way to find if a menu items should be active. After searching the internet i found a few options*, but none of them did fit my needs, so i wrote my own: Usage: <a href="{% url 'view-name' %}" class="{% current request 'view-name' %}"></a> * http://gnuvince.wordpress.com/2008/03/19/the-new-and-improved-active-tag/ * http://stackoverflow.com/questions/340888/navigation-in-django

  • template
  • path
  • active
  • link
  • current
Read More

decorator for decorators with optional args

Have you ever wanted a decorator that you could apply either straight-out: @mydec def myfun(...): ... or with special arguments: @mydec(special=foo) def myfun(...): ... ? Well, decorate it with this metadecorator, and voila. (I had this idea independently, but it's been done before as decorator_withargs: http://osdir.com/ml/python.ideas/2008-01/msg00048.html. My version is actually useful because it deals with signatures and calling directly.) As http://www.siafoo.net/article/68 points out, the standard decorator module has too much magic: the "@decorator" decorator expects a wrapping function, not a working decorator. This module fixes that.

  • decorator
  • decoratordecorator
  • metadecorator
Read More

Cached model property decorator (like @property)

This is a nice decorator for using the cache to save the results of expensive-to-calculate but static-per-instance model properties. There is also a decorator for when the property value is another model, and the contents of the other model should not be cached across requests. 3 levels of caching implemented: * outermost: django cache * middle: obj._cache (for multiple properties on a single object) * innermost: replace the attribute on this object, so we can entirely avoid running this function a second time.

  • property
  • cache
  • decorator
Read More

Custom Times for the Django Admin Time Widget

Based on code from [mihelac.org](http://source.mihelac.org/2010/02/19/django-time-widget-custom-time-shortcuts/) Modified to work in Django 1.3.1. Put it in templates/admin/app_label/model/change_form.html

  • template
  • javascript
Read More
Author: caa
  • 0
  • 1

Decorate every view in a url tree

Add login_required (or any other combination of decorators) to any view references by the urls created by patterns(...). My personal little itch as an example... urlpatterns += required( login_required, patterns('', (r'^api/', include(api.urls)), ) )

  • urls
  • login_required
Read More

Active Directory Authentication Backend (with User object updating)

I used the code from http://djangosnippets.org/snippets/901/ and expanded the code to have the possibility to map AD groups to the superuser attribute of Django's users. The code updates the Django users database every time a user connects, so every change in the AD is replicated to the Django database.

  • Active Directors
  • Auth
Read More

Dynamical formsets

If you need dynamical formsets you can use `{{ empty_form }}` in template and even makeup it.

  • formset
Read More

3110 snippets posted so far.