How to PUT a file in Django
Python, Web January 28th, 2009
Once we decide to go for PUT instead of POST, we step out the comfort zone of django, there is no mapped form filed, no validation, we have to deal with the raw WSGI interface by ourselves. Anyway, we can still use the the django.core.file.File.
If we dig into the source code, the django.core.file.File defines: open, close, read, tell, seek, flush and some other django-specific operations, like chunks, readlines, xreadlines etc. Ticket #8501 glues File and file object when chunks method is missing.
It is interesting that the interface File exposed explicitly requires that the underlying file object supports random access, which is most likely overqualified for general use. Sometimes, less is more. And it implicitly expects read will return EOF, which is also not true for WSGI.input. So we end up to brew our own:
# Only forward access is allowed
def __init__(self, socket, size):
super(SocketFile, self).__init__(socket)
self._size = int(size)
self._pos = 0
def read(self, num_bytes=None):
if num_bytes is None:
num_bytes = self._size – self._pos
else:
num_bytes = min(num_bytes, self._size – self._pos)
self._pos += num_bytes
return self.file.read(num_bytes)
def tell(self):
return self._pos
def seek(self, position):
pass
The SocketFile object is initialized with the length of the socket file object, aka CONTENT_LENGTH, the read method gatekeeps the operation to return EOF. seek is inherited from File, so just bypass it. Just wrap the raw WSGI.input with SocketFile, and use it as File. Please check views.py for the usage.







what’s the use case or benefit for using PUT ?
Just for the sake of semantics: GET and PUT, seems very RESTful.
@felix, I would recommend you to read the RESTful column by Joe Gregorio. http://www.xml.com/pub/at/34
HTTP offers a series of tools to manage your information space (note that I have written information space and not files). Among these tools there are PUT, DELETE, GET, etc. PUT and DELETE should be the first verb taught to developers more than GET and POST. It makes somehow more sense.
The support of HTTP PUT has never been high because of the lack of support in recent browsers and apache. You need to rely on third parties tool, modules. In an environment of you have control on clients/servers design, then you have a set of very powerful tools to manage your information space.
it looks great to me i am curious to know further.
Never used Django before. Thanks for sharing!
seems Django is a good system.