Login

All snippets

Snippet List

Admin Save and view next button

Add a Save and view next button to your admin change form. Put the code at [link](http://www.djangosnippets.org/snippets/2006/) in a file called myapp/templates/admin/myapp/change_form.html Note: Requires Django 1.2.

  • admin
  • view
  • save
  • button
  • next
  • 1.2
Read More

Vertical and Horizontal Line to geraldo report

It's fixed the right and bottom for each use. A vertical line is a line with the same right and left, so it fixed the right attribute to be the same value of the left attribute. A horizontal line is a line with the same bottom and top, so it fixed the bottom to be the same value of the top attribute.

  • geraldo
  • report
Read More

strip_tags like php one

Usage: `clean_html = strip_tags(html, {'a': ['href'], 'p': ['class']})` Based on [another snippet](http://www.djangosnippets.org/snippets/205/).

  • tags
  • html
Read More

Mobile browser detection middleware

This middleware adds a "is_mobile" (boolean) to the request object if the user's browser is a mobile browser (iPhone, Nokia, etc). **Example of use inside a view:** `request.is_mobile` **Example of use inside a template:** *You must activate the template processor "django.core.context_processors.request" in your settings. (see TEMPLATE_CONTEXT_PROCESSORS at djangoproject.com)* `{{ request.is_mobile }}`

  • middleware
  • detect
  • browser
  • detection
  • mobile
Read More

Quick script to convert json data to csv

I often need to dump data from a database to csv. This little snippet should make it easy enough to do without having to worry too much about character encodings, though it does assume you want your csv file to be utf-8 encoded. Note that this dumps just one table from the database. Trying to dump all the tables in your app will raise an exception.

  • json
  • csv
  • utility
Read More

Dojo Helper

This module contains functions and classes that help integrate the Dojo Toolkit javascript library with Django. Supports defining theme, stylesheets, required modules, and addOnLoad functions from Django. Django form fields can be instrumented to be instantiated as Dijit form objects programmatically. Full instructions on [limscoder.com](http://www.limscoder.com/2010/04/django-dojo.html).

  • ajax
  • javascript
  • dojo
Read More

datetime.time/datetime.datetime to Unix Epoch (with microsecond support)

This is useful when you need to convert a datetime.datetime.now() or datetime.date.today() into a unix epoch seconds, with microsecond precision (precision only applies to datetime.datetime, as datetime.date won't have any microseconds). I have found this is necessary for when storing the DateTime in the models as a FloatField, in order to keep the usec/microsecond precision. At some point, I will probably create a custom model field called DateTimeWithUSec or something like that, but for now, this will do :)

  • datetime
  • date
  • time
  • epoch
  • convert
  • unix
  • usec
  • precision
  • microsecond
Read More

Smart widthratio

This is the same as [{% widthratio %}](http://docs.djangoproject.com/en/dev/ref/templates/builtins/#widthratio) but when the given value is greater than the max_value it just uses the max_value. This way the result is never greater than max_value. It also returns max_width if the max_value is 0 instead of returning a blank string. Usage example: {% smart_widthratio this_value max_value 100 %}

  • templatetag
  • widthratio
  • templatetags
Read More

DKIM Email Backend

Overrides the `_send` method of the default SMTP `EmailBackend` class to include a [DKIM](http://www.dkim.org/) signature based on settings: 1. `DKIM_SELECTOR` -- e.g. `'selector'` if using `selector._domainkey.example.com` 2. `DKIM_DOMAIN` -- e.g. `'example.com'` 3. `DKIM_PRIVATE_KEY` -- full private key string, including `"""-----BEGIN RSA PRIVATE KEY-----"""`, etc You'll need [pydkim](http://hewgill.com/pydkim/). Just include this code in `project_name.email_backend.py`, for example, and select it in your settings file; e.g. `EMAIL_BACKEND = 'project_name.email_backend.DKIMBackend'`

  • email
  • dkim
Read More

ISBN model field: displays 10- and 13-digit variants and external links

Requires [PyISBN](http://pypi.python.org/pypi/pyisbn/0.5.2). Use like so: class Book(models.Model): title = models.TextField() isbn = ISBNField() ... the link in the widget can be changed to amazon, borders, you name it. If the DB version is a 13-digit ISBN, the display box contains the 10-digit, labeled; and vice-versa.

  • django
  • model
  • python
  • widget
  • modelfield
  • isbn
  • books
Read More

Email queue in DB

This is what I use to send simple status emails from my sites. Instead of a django.core.mail.send_mail call, which can take an irrritatingly, nondeterministically long time to return (regardless of error state), you can stow the emails in the database and rely on a separate interpreter process send them off (using a per-minute cron job or what have you). You then also have a record of everything you've sent via email from your code without having to configure your outbound SMTP server to store them or anything like that. Usage notes are in the docstring; share and enjoy.

  • django
  • python
  • email
  • mail
  • database
  • queue
  • asynchronous
Read More

Easier prefix handling for forms

**autoprefixed** is a decorator for Form classes that simplifies prefix handling by storing it in a hidden field. Thus when the form is posted, the prefix can be extracted from the posted data instead of having to pass it explicitly when instantiating the form.

  • form
  • prefix
Read More

3109 snippets posted so far.