Bones' Blog of Stuff About Things

15 Jun

Image Conscious

So, Django project number two is underway. I wanted to give Django another chance, and with the game stuff not really using it very much I pulled another web-app idea from my file and decided to give it a try with the latest development version of Django. The concept is fairly standard web-stuff. Users can tag other web-content in various ways and then build their own view on it. It’s intended for a very specific purpose (which I won’t explain yet), but at its heart it is another search-engine/book-marking idea.

I’ve chosen to use PostgreSQL for a change, which threw up a problem or two with the psycopg Python packages. The problem is that psycopg has progressed to version 2, but hasn’t had an official release of that version and so Django doesn’t support it by default. Once you work out the version issue, you have to work out how to build version 1, which is something of a pain. No big deal, perhaps, but a newbie to Django could easily be entirely discouraged by such things.

Anyway, I’m made of sterner stuff and so I have the PostgreSQL install all working away. The problem I’m trying to solve at the moment involves the use of ImageField. I’ve created a model entity that represents an Image and uses a URLField and an ImageField. The idea is that the user will provide the image by means of a URL and that the file will then be pulled onto my server where I will do some processing and store the processed image on my server. So, my first step was to try and alter the admin so that I could add one of these records, filling out the url field and then do the retrieval and processing when I click on the save button.

After some time looking through the docs, the code and the web, I was really no wiser as to exactly how to achieve this. My own sense of OO suggests doing it in the overridden save method in the model class, and a plea for help on #django got a similar suggestion, prompting me to try the following as a naive proof of concept:

def save(self): f = urllib.urlopen(self.url) rawimagedata = f.read() self.savefullbitmapfile(‘filename.jpg’,rawimage_data) super(Image, self).save()

That turned out to produce a recursion, as savefullbitmap_file() calls the save() method. However, even when I blocked the recursion with a simple test, the process failed with this error:

Exception Value: [Errno 36] File name too long: ‘/site/filename___________________________






___________________________.jpg’

The Django code adds underscores repeatedly if the file is reported as already existing, but the directory is empty. Permissions seem fine and so I’m a bit stumped as to what is wrong. It doesn’t help that I’m entirely unsure if I’m going about what I’m trying to do in a vaguely sensible manner. Django is generally very consistent and presents a fairly intuitive structure, but right now it has me confused.

2 Responses to “Image Conscious”

  1. 1
    Jeroen Ruigrok/asmodai Says:

    psycopg 2.0.2 has been released on 11 June and is marked as stable. Django supports it since a few weeks already.

  2. 2
    mwtb Says:

    I took the Django code from trunk on June 4th, so if it has been supported for “a few weeks”, I don’t quite know why it doesn’t work for me. I’ll get the latest version this evening and see what happens. Thanks for letting me know.

Leave a Reply

© 2010 Bones' Blog of Stuff About Things | Entries (RSS) and Comments (RSS)

GPS Reviews and news from GPS Gazettewordpress logo