Until recently, I'd swerved the image parts of my Django web app as it seemed it would be as much effort customising the field for the development server as it would for a full apache instance. (However with hindsight, I'm not so sure). Here are my steps to get a model / view and templates working with an ImageField.

First you need the right configs in settings.py:

MEDIA_ROOT = '/home/username/webapps/media'
MEDIA_URL = 'http://username.hosting.com/media/'

The root setting is where on the filesystem the base directory where the images/files will be uploaded to. media_url is the corresponding url where a client can request the files from.

In the model to store the image, you need some field

picture = models.ImageField(upload_to='userpics/')

The upload_to value will be a sub-directory of /home/username/webapps/media

To capture an image from user input, the form's attribute just needs a

picture = forms.ImageField()

The form in a template is a little different from other forms as it needs an additional attribute to cope with the file being uploaded:

<form action="." method="POST" enctype="multipart/form-data">
<table>
<tr>
<td>Picture</td>
<td></td>
</tr>
</table>
<input type="submit" value="Submit"/>
</form>

The final part is the view which changes slightly to accept a dictionary of files

def photo_input(request):
  if request.method == 'POST':
    form = PhotoForm(request.POST, request.FILES)
    if form.is_valid():
      profile = User.objects.get('noel').get_profile()
      profile.picture = request.FILES['picture']
      profile.save()
      return HttpResponseRedirect('/next/page/')
  form = PhotoForm()
  return render_to_response(
      'photo_adder.html',
      {'form': form },
      RequestContext(request))

And now the user-profile object has an image or file referenced to it, stored on the filesystem under

/home/username/webapps/media/userpics/

This file can then be used in a template:


Hope this helps someone. If you have any feedback, please let me know.