Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango REST Framework POST data with file error (using ModelResource)
    primarykey
    data
    text
    <p>I have a really big problem trying to post data with file in a Django REST Framework app. I've created a simple application by the example at djangorestframework website. So I have the urls file:</p> <pre><code>class MyImageResource(ModelResource): model = Image </code></pre> <p>and in urlpatters:</p> <pre><code>url(r'^image/$', ListOrCreateModelView.as_view(resource=MyImageResource)), url(r'^image/(?P&lt;pk&gt;[^/]+)/$', InstanceModelView.as_view(resource=MyImageResource)), </code></pre> <p>The Image model is simple:</p> <pre><code>class Image(models.Model): image = models.ImageField(upload_to=get_file_path) name = models.CharField(max_length=256, blank=True) description = models.TextField(blank=True) </code></pre> <p>Testing the REST page in browser, works perfect. Even posting data with files.</p> <p>My problem is that I want to create a simple python application to post data. I've used simple urllib2, but I get 500 Internal Error or 400 Bad Request:</p> <pre><code>poza = open('poza.jpg', 'rb') initial_data = ( {'name', 'Imagine de test REST'}, {'description', 'Dude, this is awesome'}, {'image', poza}, ) d = urllib.urlencode(initial_data) r = urllib2.Request('http://localhost:8000/api/image/', data=d, headers={'Content-Type':'multipart/form-data'}) resp = urllib2.urlopen(r) code = resp.getcode() data = resp.read() </code></pre> <p>I've tried also with MultipartPostHandler:</p> <pre><code>import MultipartPostHandler, urllib2, cookielib cookies = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), MultipartPostHandler.MultipartPostHandler) params = { "name":"bob", "description":"riviera", "content" : open("poza.jpg", "rb") } opener.open("http://localhost:8000/api/image/", params) </code></pre> <p>but same: 500 or 400 errors and the server (python manage.py runserver) stops with the following errors:</p> <pre><code>Exception happened during processing of request from ('127.0.0.1', 64879) Traceback (most recent call last): File "C:\Python27\lib\SocketServer.py", line 284, in _handle_request_noblock self.process_request(request, client_address) File "C:\Python27\lib\SocketServer.py", line 310, in process_request self.finish_request(request, client_address) File "C:\Python27\lib\SocketServer.py", line 323, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 570 , in __init__ BaseHTTPRequestHandler.__init__(self, *args, **kwargs) File "C:\Python27\lib\SocketServer.py", line 641, in __init__ self.finish() File "C:\Python27\lib\SocketServer.py", line 694, in finish self.wfile.flush() File "C:\Python27\lib\socket.py", line 303, in flush self._sock.sendall(view[write_offset:write_offset+buffer_size]) error: [Errno 10053] An established connection was aborted by the software in yo ur host machine </code></pre> <p>If somebody have, please give me an example of posting data with files or tell me what's wrong in my posting python code. I couldn't find any more examples.</p> <p>The server looks ok, I can POST data in browser. Thank you very much.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload