Login

All snippets written in Python

Snippet List

Fake SSL Middleware for Tests and Local Development

Add `FakeSSLMiddleware` to the top of your `MIDDLEWARE_CLASSES` stack when running tests or developing locally to allow https:// links to operate correctly. Can be used in conjunction with other SSL middleware to allow critical tests to be performed.

  • middleware
  • ssl
  • testing
  • test
  • https
  • local
  • fake
Read More

Bitwise operator queryset filter

This snippet for django-1.2 allows you to use bitwise operators without using QuerySet.extra() from django.db.models import * from somewhere import FQ class BitWise(Model): type = CharField(max_length=8) value = IntegerField() def __unicode__(self): return '%s - %d' % (self.type, self.value) >>> BitWise.objects.create(type='django', value=1) <BitWise: django - 1> >>> BitWise.objects.create(type='osso', value=3) <BitWise: osso - 3> >>> BitWise.objects.create(type='osso', value=7) <BitWise: osso - 7> >>> BitWise.objects.filter(FQ(F('value') & 1, 'gt', 0)) [<BitWise: django - 1>, <BitWise: osso - 3>, <BitWise: osso - 7>] >>> BitWise.objects.filter(FQ(F('value') & 2, 'gt', 0)) [<BitWise: osso - 3>, <BitWise: osso - 7>] >>> BitWise.objects.filter(FQ(F('value') & 1, 'gt', 0) & Q(type='django')) [<BitWise: django - 1>]

  • filter
  • queryset
  • bitwise
  • operator
Read More

Improved User Admin

Helper function which adds some niceties to the auth/user admin views. Needs django 1.2 to work. ### Usage Define a UserProfile class and set `AUTH_PROFILE_MODULE` as per the [django docs](http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users) In your admin.py file (or anywhere else) add the following: from models import UserProfile from [path to snippet] import upgrade_user_admin upgrade_user_admin(UserProfile)

  • admin
  • user
  • userprofile
Read More

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

2956 snippets posted so far.