Login

All snippets

Snippet List

More readable Enumeration class for Django choices

We currently use two-level tuples to specify choices of a field in models or forms. But, because it has only (value, verbose name) pair, the readability is bad whenever we indicate a specific choice value in our Python codes. So I made a small class that does "magic" for this: A Named Enumeration. Instead of `myobj.status == 0`, use `myobj.status == STATUS.UNREVIEWED`, for example.

  • choices
  • model
  • orm
  • enumeration
  • enum
Read More

StateField

Based on [CountryField](http://www.djangosnippets.org/snippets/494/).

  • model
  • field
  • state
  • statefield
Read More

Template tags to integrate with modconcat

Assumes mod_concat is installed: http://code.google.com/p/modconcat/ Django template tags that combine blocks of CSS and Javascript into modconcat friendly URLs. Takes this: `{% cssconcat "{{ MEDIA_URL }}css/" %} <link href="{{ MEDIA_URL }}css/a.css" rel="stylesheet" type="text/css" /> <link href="{{ MEDIA_URL }}css/b.css" rel="stylesheet" type="text/css" /> <link href="{{ MEDIA_URL }}css/c.css" rel="stylesheet" type="text/css" /> {% endcssconcat %}` And outputs this: `<link href="/site_media/??a.css,b.css,c.css" rel="stylesheet" type="text/css" /> ` Similarly for javascript: `{% jsconcat "{{ MEDIA_URL }}js/" %} <script src="{{ MEDIA_URL }}js/a.js" type="text/javascript"></script> <script src="{{ MEDIA_URL }}js/b.js" type="text/javascript"></script> <script src="{{ MEDIA_URL }}js/c.js" type="text/javascript"></script> {% endjsconcat %}` becomes: `<script src="/site_media/??a.js,b.js,c.js" type="text/javascript"></script> `

  • javascript
  • css
  • modconcat
  • mod_concat
Read More

Show users' full names for foreign keys in admin

This is a ModelAdmin base class you can use to make foreign key references to User a bit nicer in admin. In addition to showing a user's username, it also shows their full name too (if they have one and it differs from the username). **2009-08-14**: updated to handle many to many fields and easily configure whether to always show the username (if it differs from full name)

  • user
  • modeladmin
  • get_full_name
Read More

Template class to test custom tag libraries

TestableTemplate behaves just like django.template.Template, but you can give it a list of template.Libraries to load before parsing the template. This is equivalent to adding a bunch of {% load %} tags to the beginning of your template string, but you can use custom tag libraries which do not belong to Django applications' templatetags packages. This is occasionally useful in testing.

  • templates
Read More

Prevent Django newcomments spam with Akismet (reloaded)

This is a rewrite of [snippet #1006](http://www.djangosnippets.org/snippets/1006/) to use the moderation features available in Django's comments framework. This is more customizable than the signals approach and works well if other moderation features are being used. If you want to make comments that are flagged as spam become hidden instead of deleted, change the allow() method to moderate(). [See the blog post here](http://sciyoshi.com/blog/2009/jul/17/prevent-django-newcomments-spam-akismet-reloaded/)

  • akismet
  • spam
  • comments
  • antispam
  • newcomments
Read More

Custom management command to list recent admin actions

On a busy site it can be nice to have a summary of admin activity. Running this command (I call it "adminlog") generates output like this: 2009-07-10 18:06:19: pbx changed flat page: "/yay/ -- Let's All Say Yay" By default it shows the last five actions; pass it a numerical arg to show more or fewer. Run this as a cron job and you can follow a site's admin-side activity without even logging in!

  • admin
  • commands
Read More
Author: pbx
  • 1
  • 3

Confirm alert if the user navigates away without saving changes

A confirm alert is displayed if the user has made any changes to the form, and attempts to navigate away from the page without saving (click the back button, hit reload, click the breadcrumbs, logout, etc). Much like GMail's "Your message has not been sent. Discard your message?" prompt. **Uses the JavaScript helpers built into Django**, without relying on 3rd party libs like jQuery. (There might be simpler options if you're already using [jQuery](http://code.google.com/p/protect-data/) or [prototype](http://stackoverflow.com/questions/925111/activating-onbeforeunload-only-when-field-values-have-changed/1013033#1013033)). To use this, simply create a [custom admin template](http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates). For example at: *templates/admin/YOUR_APP_NAME/change_form.html* {% extends "admin/change_form.html" %} {% block after_related_objects %} {{ block.super}} <script type="text/javascript"> ... script goes here ... </script> {% endblock %}

  • admin
  • change-form
Read More

slug filename

A one-liner that I use all the time: Set `upload_to` to something based on the slug and the filename's extension. Just add this function to the top of your models and use it like this: image = models.FileField(upload_to=slug_filename('people')) and the `upload_to` path will end up like this for eg `myfile.jpg`: people/this-is-the-slug.jpg Enjoy!

  • upload_to
Read More

Profanity Check

Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc. This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.

  • tag
  • profanity
Read More

Clear FileField/ImageField files in the Admin

The widget for FileField and ImageField has a problem: it doesn't supports clear its value and it doesn't delete the old file when you replace it for a new one. This is a solution for this. It is just for Admin, but you can make changes to be compatible with common forms. The jQuery code will put an **<input type="checkbox">** tag next to every **<input type="file">** and user can check it to clear the field value. When a user just replace the current file for a new one, the old file will be deleted.

  • admin
  • jquery
  • imagefield
  • filefield
Read More

3110 snippets posted so far.