Login

Top-rated snippets

Snippet List

A basic view for working with jQuery dataTables plugin

jQuery dataTables is a fantastic and powerful client-side plugin, which has many capabilities. There are many libraries out there that easily integrate it with django with little to no effort. However, they provide everything out of the box, which isn't always the most flexible solution. I also think beginners have a lot to learn from not using already created tools. So, finding the general examples out there lacking, I though I'd hatch out a quick example of how to very basically integrate the two. I supply two lists at the top which define how to order the fields and which are searchable (quite similar to the one defined in the admin site) and later everything is figured from there. Of course, for a little more complex implementation (say using a method instead of a field) this will not work (since getattr doesn't automatically call a function if it is a function), but this snippet isn't supposed to provide everything but a very basic usage example, which can be very quickly expended in any way needed. Anyway, this snippet should work on all django versions and all dataTables versions without a hitch. last note- I use `cStringIO` with `json.dump` out of good personal experience with that settings, though it might not be the best way to serialize the data. Good luck, share use and enjoy, ~yuvi

  • django
  • dataTables
Read More

Dict Concat

Wanted a simple function to concatenate n dictionaries. Each dictionary passed in is deep copied to avoid any possible mutation of dictionaries being concatenated. Dictionary m's key/values will be overwritten by dictionary m+1's key/values.

  • dict
  • dicts
  • dictionary
  • concat
Read More

Greek uppercase tag for django

Tag to correctly uppercase Greek characters with accent, copy the code in your templatetags folder in a file of your choice, load it in your templates {% load yourfile %} and use it as {% string|gruppercase %} the code works also with other languages, it won't modify anything (eg {% ukwork|gruppercase %} as it transforms only Greek characters (unless your language contains Greek characters).

  • tag
  • django
  • templates
  • uppercase
  • greek
Read More

IntegerRangeField

Django models IntegerField that get max_value and min_value in it's constructor and validate on it. It's initialize the formfield with min_value and max_value, too.

  • models
  • fields
  • IntegerField
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

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

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

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

3110 snippets posted so far.