Login

Most bookmarked snippets

Snippet List

Admin actions as buttons instead of a menu [v2]

Add this to your admin change_list.html template to replace the actions drop-down with buttons. This is a rewritten version of snippet [1931](http://djangosnippets.org/snippets/1931/) with Django 1.3 compatibility and cleaner code. Thanks to [andybak](http://djangosnippets.org/users/andybak/) for the idea.

  • admin
  • actions
Read More

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

staff_or_404 Decorator

Sometimes I don't want to reveal a staff-only view so I created this decorator, using ``django.contrib.admin.views.decorators.staff_member_required`` as my boilerplate. Non staff members are kicked to the 404 curb. Suggestion: Create a file, ``decorators.py`` in your project (or single app) and import like so: ``from myproject.app_name.decorators import staff_or_404``.

  • user
  • auth
  • decorators
  • staff
Read More

Get the Django decorator/middleware cache key for given URL

This mimicks the keys used internally by the @cache_page decorators and site-wide CacheMiddleware. Now you can poke them, prod them, delete them, do what you like with them (eg, delete after you update some content and you want a specific URL refreshed).

  • cache
  • key
Read More
Author: s29
  • 2
  • 2

Django model objects and querysets dehydration/hydration

Dehydrates objects that can be dictionaries, lists or tuples containing django model objects or django querysets. For each of those, it creates a smaller/dehydrated version of it for saving in cache or pickling. The reverse operation is also provided so dehydrated objects can also be re-hydrated. *Example:* >>> import pickle >>> users = list(User.objects.all()[:20]) >>> print users [<User: Indiana Jones>, <User: Bilbo Baggins>, ...] >>> pickled_users = pickle.dumps(users) >>> print len(pickled_users) 17546 >>> dehydrated_users = dehydrate(users) >>> pickled_dehydrated_users = pickle.dumps(dehydrated_users) >>> rehydrated_users = hydrate(pickle.loads(pickled_dehydrated_users)) >>> print rehydrated_users [<User: Indiana Jones>, <User: Bilbo Baggins>, ...] >>> print len(pickled_dehydrated_users) 1471

  • models
  • orm
  • queryset
  • hydrate
  • dehydrate
Read More

Custom CSS class in Form with template tag filter

It was based in: http://djangosnippets.org/snippets/1586/ Instead of doing this: 'attribute_name = forms.CharField(widget=forms.TextInput(attrs={'class':'special'}))` You can do this in your template: {{ form|cssclass:"attribute_name:special_class"|cssclass:"other_attribute:special_class" }}

  • filter
  • templatetag
  • css
  • form
  • class
Read More

ModelForm Class saving m2m

**Your model:** class TicketItem(models.Model): hours = models.DecimalField(decimal_places=2, max_digits=6) day = models.DateField() order = models.ForeignKey(Order) tickets = models.ManyToManyField(Ticket) Now you want to auto save m2m fields in your forms.TicketItemCreateForm: 1. inherit from m2mForm-Class 2. define m2m_field(s) **Example:** class TicketItemCreateForm(m2mForm): m2m_field = 'tickets' class Meta: model = models.TicketItem

  • forms
  • m2m
  • class
  • modelform
Read More

Send templated email with text | html | optional files

Use this to send emails to your users, takes one template and renders it as html or text as needed. Credits to """ Jutda Helpdesk - A Django powered ticket tracker for small enterprise. (c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details. lib.py - Common functions (eg multipart e-mail) """ MIT licence I only removed the specific project parts and made it general to use. The original project repository https://github.com/rossp/django-helpdesk/

  • template
  • email
  • function
Read More

Password Validation - Require Letters and Numbers - no regex

Simple password validation for user registration - requires that password be 7 or more characters and contain both letters and numbers. Original validation with regex approach developed by kurtis. Optimized no-regex version based on code from watchedman ran as fast or significantly faster on all systems on which we tested it.

  • registration
  • model
  • regex
  • user
  • validation
  • form
  • password
Read More

Admin log entries management utils

A collection of utilities for admin log entries management. This module provides functions to add log entries for instance addition, change and deletion, as seen in *django.contrib.admin.options*. It also provides a class based view mixin to add logging capabilities, and other tools as a log collector. See docstrings for a better description.

  • admin
  • logentry
Read More

3110 snippets posted so far.