Archive for the 'Django' Category

Django File Upload Handling Examples

Wednesday, February 18th, 2009

I have been working on a multi-user blogging and publishing platform using Django 1.0 lately. Naturally this requires the backend to be able to handle file uploads. A lot of things have changed from Django 0.96 which I am still using on some legacy code. One of those changes is the way Django 1.0 handles file uploads. Most of the changes done were made to allow Django apps to handle large files without soaking up too much memory.

So what has changed? The most visible change is that there are now at least two separate API’s that you have to work with. You have the File API and the Storage API. The File API which exposes the File class provides a thin wrapper around Python file objects. The Storage API, on the other hand, exposes a base class Storage that you can use to implement custom storage facilities. There is another API that provides FileUploadHandler. This will allow you to customize the way Django handles the uploaded files in their “raw” form. For most purposes, the File and Storage API will suffice.

This post is meant to supplement the information found in the “Handling Uploaded Files” section of the Django File Uploads documentation. You will still need to refer to documentation.

(more…)

Writing a Facebook App using Django

Wednesday, December 31st, 2008

I recently found myself writing my first Facebook app using Python with the Django web framework. I have to admit that it was not a very pleasant experience. The usual Django development cycle of programming and testing locally does not work. I have a very simple workflow nailed down when writing Django apps: write, test locally, commit to VCS, and upload to server. When writing a Facebook app, I had to change the current workflow a bit: write, commit to VCS, upload to server, test on production.

Facebook, being a PHP-powered site, likes to make the assumption that you will also be writing your app using PHP. They don’t even make any efforts to hide their bias towards PHP. The “official” client library is for PHP. The example program they give to you the first time you sign up is written in PHP. All the relevant starting points all lead towards PHP-centric development.

Thankfully, there are those who realize that PHP is not the be-all and end-all to programming web applications. You can find unofficial client libraries for other programming languages. Download and install the client library for your programming language of choice and follow the documentation and hopefully you get to have everything working on your first try.

(more…)

OMG!!! Ponies!!!

Tuesday, November 4th, 2008

Now see this is why I love Django. It’s an awesome framework with a great community behind it.

Undocumented Django: Overriding _get_FIELD_url and friends

Monday, April 21st, 2008

Recently I found the need to override the _get_FIELD_url() for a particular set of models in my Django app. The _get_FIELD_*() methods provide your FileField and ImageField with “magic” convenience methods. For example:

from django.db import models

class MyModel(models.Model):
    file = models.FileField(upload_to=’some/path’)

If you want to to retrieve the URL for the file you would use:

m = MyModel().objects.get(id=1)
m.get_file_url()

Notice that your class automagically gets a method called “get_file_url()”. This is the “magic” part and all of this is done behind the scenes for you. While this is really convenient for getting things done most of the time, there are some times where you need to have more control over what’s returned by these magic methods.

(more…)