Login

All snippets

Snippet List

ContentType template filter

Custom template filter to retrieve a content type of a given model instance. Useful for ModelForms which want to set the content_type field (i.e: GenericForeignKey). ### A usage example: {% load helpers %} {% with instance|content_type as ctype %} <input type="hidden" name="content_type" value="{{ ctype.pk }}"> {% endwith %} Original idea from [this stackoverflow answer] [1] [1]: http://stackoverflow.com/a/12807458/484127

  • template
  • filter
  • tag
  • contenttypes
  • contenttype
  • GenericForeignKey
Read More

Inherit the standard url tag to include domain name

This module extends the standard `url' template tag in Django and adds support for fully qualified domain name URLs. It also can be extended with simple URL load balancing techniques if desired. See my blog for the background story: <http://atodorov.org/blog/2013/12/22/django-template-tag-inheritance-howto/>

  • template
  • tag
  • templatetag
  • url
  • inheritance
Read More

Multiple emails form field

Validate form field that include email or emails separated by 'token' kwargs, by default ',' a comma. Return a list [] of email(s). Check validity of the email(s) from django EmailField regex (tested with 1.3, but normally will also work with 1.5)

  • multiple
  • email
  • validation
  • form
  • field
  • list
Read More

Lazy context processor.

Sometimes you have context variables that are needed on many pages in a site, but not all. You only want them to be evaluated when actually needed, especially if they are expensive calculations that do DB queries etc. The pattern to use is shown: put a callable into the context, not the final value, and also wrap the callable with memoize_nullary.

  • template
  • contextprocessor
  • lazy
  • context-processor
  • memoize
Read More

Decorator to execute a method only once

Beware if using Amazon Simple Queue Service to execute Celery tasks which send email messages! Sometimes SQS messages are duplicated which results in multiple copies of the messages being sent. This is a simple decorator which uses a cache backend to prevent the task from executing twice in a specified period. For example: @task @execute_once_in(3600*24*7) def cron_first_week_follow_up(): """ Send a follow-up email to new users! """ pass For more info see <http://atodorov.org/blog/2013/12/06/duplicate-amazon-sqs-messages-cause-multiple-emails/> <http://atodorov.org/blog/2013/12/11/idempotent-django-email-sender-with-amazon-sqs-and-memcache/>

  • django
  • email
  • decorator
  • amazon
  • queue
  • celery
Read More

View all log entries in the admin 2

Based on: [View all log entries in the admin](https://djangosnippets.org/snippets/2484/) Improvements: * No crash on `object_link` when reverse route missing, * Filter by users present in the log AND in database currently * Other filters on users - only superusers / only staff (modify to fit your needs) EDIT: Incorporated `action_description` from [django-logentry-admin](https://github.com/yprez/django-logentry-admin). * The list filter now has human-friendly action names, and the table also. * Refactored list filter class hierarchy.

  • admin
  • list-filter
  • logentry
Read More

Load Template from App

Updated a similar snippet by "King" to work with Django 1.6. This is especially useful for overriding the admin templates without having to symlink or copy them into your project. For example {% extends "admin:base.html" %} would extend the admin page base.html.

  • template
  • admin
  • loader
  • app
Read More

Admin action for a generic "CSV Export" (fix for unicode)

Based on [#2369](https://djangosnippets.org/snippets/2369/) Save the snippet as actions.py within your django app, and then add an action on any model you want in it's ModelAdmin definition. Example usage: from actions import export_as_csv_action class YourModelAdmin(admin.ModelAdmin): list_display = (...) list_filter = [...] actions = [export_as_csv_action("CSV Export", fields=[...])] - Unicode fix (requires unidecode)

  • admin
  • export
  • csv
  • action
Read More

regex search in admin forms

Despite warning coming from django developers, I'm still using admin classes to quickly get into reverse engineering databases. One feature is missing: searching into fields thanks to a regex. One dirty solution I found is to overwrite get_search_results. But most of the code comes from django itself. If anyone has a better idea ;) **Usage:** 1. works since get_search_results is part of ModelAdmin (1.5 if I remember well) 2. Inherit your Admin class from RegexModelAdmin 3. enclose by / the field you want to regex with: `search_fields = ['/field/', ]`

  • admin
  • search
  • regex
  • field
Read More

Django admin autoregister

Official GitHub page: https://github.com/Mimino666/django-admin-autoregister One call to autoregister_admin() automatically creates and registers admin views for all the models in the specified module with intelligent linking between ForeignKey, OneToOneField and ManyToManyField fields.

  • models
  • admin
  • auto
  • autoregister
Read More

3109 snippets posted so far.