You don't have to use Django to serve the static content directly. Simply have your server route 404 requests for your images folder to a Django view, where it splits apart the filename and generates the appropriate thumbnail, before redirecting back to the original URL (which hopefully will no longer be a 404).
As for the other answer's django-imagekit suggestion, I'm not sure it does anything to let you dynamically generate image thumbs based on URL, but I certainly do recommend using it for all the features it does have.
Edit:
As for the actual URL structure, I feel a more typical /images/filename-120x45.jpg
would allow you to more easily filter out 404 requests that have nothing to do with dynamic thumbnail generation. Say, for instance, that there are tons of 404 errors for /images/original_size_image.jpg
. You wouldn't want those being routed to Django, and you could only match filenames of that format with regex. [end edit]
You have to be careful though about letting anybody aware of this feature spam your Django app. They could potentially kill it with an infinite number of image size and filename combinations at their fingertips. You would need to figure how to put upper limits on these requests, like redirecting back to a 404 if either dimension is larger than the original, or even figuring out how to cap requests for multiple dimensions of the same image. Maybe this was what you were you getting at when mentioning "locking" though.
As an aside, I see you've tagged Apache but I would really like to recommend that you serve static content through something like Nginx. You could maybe negate the extra overhead of the dynamic image requests if you use a static file server that isn't complete crap at serving static files.