date: 2012/11/25
This is a simple django-rest-framework multipart/form-data example.
Setup was on ubuntu 12.04.
*Django==1.4.2 *Pygments==1.5 *argparse==1.2.1 *djangorestframework==2.1.6 *wsgiref==0.1.2 *requests==0.14.2a
Download and cd into the multipart-form-data_django_rest_framework_tutorial:
$ cd multipart-form-data_django_rest_framework_tutorial/
Install requirements from the r.txt file with pip:
$ pip install -r requirements.txt
Setup database:
$ python manage.py sncdb
Run the Django development server:
$ python manage.py runserver
This files provides commands to upload files to the django-rest-framework api.
Open a python interpreter:
$ python
Import requests in python interpreter:
$import requests
Swap out the YOUR_URL in the url variables with your root url:
$url_upload_form = 'YOUR_URL/api/upload_form/'
$url_upload_serializers = 'YOUR_URL/api/upload_serializers/'
Paste url variables into python interpreter:
Create a file to up:
$files = {'docfile': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
Upload a file with a form:
$r=requests.post(url_upload_form, data={'title':'file_upload_form'}, files=files)
Upload a file with django-rest-framework serializer:
$r=requests.post(url_upload_serializers, data={'title':'file_upload_serializers'}, files=files)
To view uploaded data in the django-rest-framework view go to:
$YOUR_URL/api/upload_form/
or
$YOUR_URL/api/upload_serializers/