Gorgeous CSS3 buttons inspired by Aqua

Modern browsers can display exciting visual effects such as drop shadows (without the use of background images). CSS3 makes it possible to turn submit inputs and even links into rich, Aqua-like buttons in these browsers (alternative style rules can be provided for older browsers).

Get attributes of Django model or instance

What is the best way to get the attributes of a Django model or instance?

from django.db import models

class Musician(models.Model):
	first_name = models.CharField()
	last_name  = models.CharField()
	instrument = models.CharField()

One option is to use __dict__.keys():

>>> m = Musician(first_name='Norah', last_name='Jones', instrument='piano')
>>> print m.__dict__.keys()
>>> ['last_name', 'instrument', 'first_name', 'id']

Another options is to use _meta.fields:

>>> print [f.name for f in m._meta.fields]
>>> ['id', 'first_name', 'last_name', 'instrument']

This approach also works on models directly:

>>> print [f.name for f in Musician._meta.fields]
>>> ['id', 'first_name', 'last_name', 'instrument']

Advantages of using _meta.fields

  • items in returned list are correctly ordered
  • applicable to both models and instances
  • only fields are returned

The fact that only fields are returned is extremely useful. Django appears to add its own attributes to instances in certain circumstances; using _meta.fields prevents these from interfering with one's own code.

Resize browser window to match iPhone viewport dimensions

I've recently become interested in optimizing sites for the iPhone and iPod touch. While nothing beats testing on the device itself, I often find it quicker to test changes on my Mac. Changing the user agent string is a piece of cake in Safari (Develop > User Agent > Mobile Safari) but what about adjusting the browser window's dimensions to match those of the iPhone?

I've created two bookmarklets to allow the current page to be loaded in an iPhone-sized window with a single click:

  • Portrait (labelled "⁑")
    javascript:open(location,'iPhone:portrait','innerWidth='+(320+15)+',innerHeight='+(480+15)+',scrollbars=yes');
  • Landscape (labelled "**")
    javascript:open(location,'iPhone:landscape','innerWidth='+(480+15)+',innerHeight='+(320+15)+',scrollbars=yes');
iPhone testing bookmarklets
iPhone testing bookmarklets: portrait and landscape

You'll find older posts in the archives.