Multiple Base Templates in Django

I’ve been working on a pretty big project at work that’s heavily oriented towards mobile devices. We have a Django site running with a lot of custom ecommerce code, but wanted to serve up an optimized experience for mobile devices.

There are a lot of ways to do this, but ultimately we wanted to serve up a different base template for visitors on small-screen mobile devices. We wrote a piece custom view middleware that sets request['is_mobile'] = True. Unfortunately, Django currently requires any {% extends %} template tag to be the first tag in your template. This means you can’t do something like this:

{% if request.is_mobile %}
    {% extends 'mobile_base.html' %}
{% else %}
    {% extends 'default_base.html' %}
{% endif %}

After racking my brain all day my coworker suggested trying the yesno template filter. It worked like a charm!

{% extends request.is_mobile|yesno:"mobile_base.html,default_base.html" %}

So in our view templates we now do {% extends 'base.html' %} which then loads the proper base template depending on the status of is_mobile.

upaste

After a brief and torrid affair with Fedora, I gave up and threw Ubuntu 10.04 on my home desktop. One thing that I missed from Fedora was the fpaste utility. This handy little command-line tool lets you paste the results of your shell commands to fpaste.org, a semi-official Fedora pastebin.

After realizing that pastebin.com had an open API, I took it upon myself to write a little Python script that could submit pastes for me.

And so I give you upaste (u for Ubuntu, but it can run on any *nix flavor, including OS X). Please try it out and give me your thoughts. I hope someone will find it useful.

Installing

Download the source from bitbucket.

# Copy the upaste file somewhere in your $PATH
$ sudo cp upaste /usr/local/bin/
 
# Then run!
$ ifconfig | upaste
 
# -h is for help
$ upaste -h
Usage: upaste [options]
  Posts a file to http://pastebin.com/ and returns the URL.
  If no file arguments are given, input is expected on stdin.
 
Options:
  --version           show program's version number and exit
  -h, --help          show this help message and exit
  -p, --private       mark your paste private.
  -f FILENAME         filename whose contents you wish to paste.
  -n PASTE_NAME       add your name or a title to paste.
  -e PASTE_EMAIL      send a confirmation email with paste link.
  -s PASTE_SUBDOMAIN  use a pastebin subdomain.
  -x PASTE_EXPIRE     set paste expiration: N = Never, 10M = 10 Minutes, 1H =
                      1 Hour, 1D = 1 Day (default), 1M = 1 Month.
  -l PASTE_FORMAT     define the default syntax highlighting for your paste.