Login

All snippets

Snippet List

Simple Mobile Support

For those interested in making a mobile site geared toward the higher end devices, and wanting a little leverage over device-specific quirks. These are the big players in the U.S. market, but of course, add your own User-Agents to match your audience's popular browsers. Usage: <html class="{{ device.classes }}"> You can also leverage template logic: {% if device.iphone %} <p>You are browsing on {% if device.iphone = "iphone4" %} iPhone 4 {% else %} an iPhone pre-version 4{% endif %} </p> {% endif %}

  • mobile
  • context_processors
Read More

X-Sendfile static file serve view

This view has the same functionality as Django's builtin static view function but when the configuration option `USE_XSENDFILE = True` is specified in the settings it returns the absolute path of the file instead of its contents. This allows the mod_xsendfile module in Apache or Lighttpd to take care of efficiently serving the file while still allowing for Django to take care of other processing of the request, like access control or counting the amount of downloads.

  • static
  • xsendfile
Read More

Improved command to generate UML diagrams of whole project or specified apps

example of use: 1. python manage.py yuml yourapp yoursecondapp --scruffy -s 75 -o test.png 2. python manage.py yuml justoneapp --scruffy -o test.pdf 3. generate whole project yuml : python manage.py yuml -a -o test.jpg 4. python manage.py yuml auth contenttypes sessions admin -o test.pdf [github repository](http://github.com/dzhibas/django-yuml)

  • django
  • command
  • basecommand
  • uml
  • yuml
Read More

Generate ICalendar Files with django-events

This is a fairly straightforward view to generate iCalendar (.ics) files, with a unique UUID, a proper filename, and the basic fields needed to export an event from a public calendar (using the django-events-calendar app). While it can certainly be extended and adapted, it works very well as-is.

  • calendar
  • ical
  • icalendar
Read More

Admin Word Count

I'm on my own personal writing challenge (see 750words.com for inspiration). I needed a way to get a dynamic word count, so I threw this together. Tied into django-basic-blog easily.

  • jquery
  • change-form
  • word-count
Read More

MoinMoin auth backend

This snippet implements an authentication backend for MoinMoin user accounts. If you have a MoinMoin running on the same server which has users, you can allow those users to sign into a Django site with the same username and password. To use, define the following settings: MOIN_DATA_DIR = "/path/to/moinmoin/data/dir" AUTH_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', '<this snippet module>.MoinMoinBackend', ) # optional list of groups that authenticating users must be in MOIN_AUTH_GROUPS = ["EditorGroup",]

  • auth
  • backend
  • moinmoin
Read More

Link raw_id_fields (both ForeignKeys and ManyToManyFields) to their change pages

**UPDATE: Now works in Django 1.4** Based on luc_j:s snippet (http://djangosnippets.org/snippets/2108/) to show values in ManyToManyFields in the admin. This snippets does that, but also links each value to its corresponding admin change page. To use, just set the raw_id_fields to the value you want, and let your form inherit from ImproveRawIdFieldsForm.

  • admin
  • widget
  • django-admin
  • raw_id_fields
  • ManyToManyField
  • ForeignKey
Read More

Polymorphic inheritance ala SQLAlchemy

This is a different take on polymorphic inheritance, inspired by SQLAlchemy's approach to the problem. The common Django approach (e.g. snippets 1031 & 1034, [django_polymorphic](http://github.com/bconstantin/django_polymorphic)) is to use a foreign key to `ContentType` on the parent model and override `save()` to set the right content type automatically. That works fine but it might not always be possible or desirable, for example if there is another field that determines the "real type" of an instance. In contrast this snippet (which is actually posted and maintained at [gist.github](http://gist.github.com/608595)) allows the user to explicitly specify the field that determines the real type of an instance. The basic idea and the terminology (`polymorphic_on` field, `polymorphic_identity` value) are taken from [SQLAlchemy](http://www.sqlalchemy.org/docs/orm/inheritance.html). Some other features: * It works for proxy child models too, with almost no overhead compared to non-polymorphic managers (since there's no need to hit another DB table as for multi-table inheritance). * It does not override the default (or any other) model Manager. Regular (non-polymorphic) managers and querysets are still available if desired. * It does not require extending a custom Model base class, using a custom metaclass, monkeypatching the models or any kind of magic.

  • inheritance
  • polymorphic
Read More

HTML Email with Inline Attachments (images)

This will allow you to attach HTML multipart emails (HTML/text) and use inline images. In my example, I'm attaching an image that's stored as an 'attachment' to an 'event.' The file name of the attachment is called "inline.jpg" and I'm referencing it in my HTML message. I'm also attaching a VCAL file (.ics) file that has some information about an associated event.

  • email
  • html
  • related
  • mixed
  • images
  • inline
  • multipart
Read More

ReportBug() with tons of debug in mail

This basically takes the debug you get from setting debug=True, but instead, pipes it into an email and sends it over to you. I have extracted this out of our de framework, it should work, but some modifications may be necessary.

  • mail
  • report
  • bug
  • mail_admins
  • reportbug
Read More

3109 snippets posted so far.