Sunday 19 July 2009

Django ImageFields in forms

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>{{ form.picture }}</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))


Sorry for the poor rendering. Problem with Blogger. Here is indented version: http://pastebin.ca/1500545

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:
{{ user.get_profile.picture.url }}

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

Thursday 2 July 2009

EuroPython Vows - Where to after the map/filter functions

As I was saying to someone at the EuroPython conference; I had been struggling as a Python programmer to get beyond the map, filter (and reduce) functions. Functional programming "done", I just didn't know where to go next to apply more learning having reached this glass ceiling.

After three days of overwhelming talks, the list is now ballooning with new areas for me to approach.

Russel Winder's talk on Gil was a warning about difficult methods to parallelise code and recommended the multiprocessing module.

From a technologies perspective, Esteve Fernandez's talk Twisted, AMQP and Thrift was the most provoking to give-a-try in some free time. I've always felt daunted by the complexity of Java RMI tools, Twisted, AMQP and Thrift would surely struggle to be more complex than their Java cousins.

Rob Collins had a talk on his FilterPype framework which seemed like a fantastic development of the piping mechanism in unix. It was this talk which started the "I remember something about that from uni" klaxons going off and Bruce Eckel continued in this, during in his presentations.

The main theoretical subjects which I heard mentioned again and again, but feel ashamed to know little about were:
  • Continuations
  • Subroutines
  • Closures
  • Generator expressions
I also heard the following Python tools / techniques mentioned and should investigate these
  • wsgi
  • zope
  • @memoize decorator to do as haskell does when first calculating factorial of 6 and then 7 after (factorial(7) does 7 * _cached_factorial(6))
  • (Also Lua, a different language)
David Jones' What Sucks About Python was another great talk on positive (and some negative) features of python where he demonstrated continuation issues which I need to follow on my own interpreter.

(I also heard Kamaelia sporadically for concurrency)

A thoroughly worthwhile use of three days. I am sure the event will help me develop as much as a programmer as a pythonista

Saturday 13 June 2009

Firefox 3.5 on Ubuntu Intrepid Ibex (8.10)

I had a small amount of hassle trying to find instructions for an easy way to install the new 3.5 version of firefox on an Intrepid version of ubuntu. Here is what worked for me...

1) System > Administration > Software Sources > Third-Party Software tab

2) Add this repository to the list:
deb http://ppa.launchpad.net/fta/ubuntu intrepid main
I think this should be the same for jaunty jakalope too i.e.
deb http://ppa.launchpad.net/fta/ubuntu jaunty main

3) sudo apt-get update

4) sudo apt-get install firefox-3.5

Firefox 3.5 is now installed here:
/usr/lib/firefox-3.5b99

5) Go to this folder and make a shortcut by dragging "firefox-3.5" to somewhere on the navigation bar.

If there's any problem with this, please let me know