I have a django file model that has a models.FileField field and a form that is used to upload files to the server:
class UploadFile(model.Model):
filename = models.FileField(upload_to='uploads')
description = models.CharField(max_length=38, blank=True)
class UploadFileForm(ModelForm):
class Meta:
model = UploadFile
fields = ('filename', 'description')
This is how the view function looks like:
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
new_file = form.save()
Now I would like to have a python script that uses only the standard library to upload files to the UploadFile model using the view function above. However this code does not work because the POST request does not have a FILES method.
import urllib, urllib2
data = urllib.urlencode({'filename': open(uploadfile, "rb"),
'description': 'upload test'})
post_req = urllib2.Request(upload_file_url, data)
result = urllib2.urlopen(post_req)
How could I add a FILES method to the POST request to upload files using a python script? I have also tried to write a different view function that does not used the form but it is not working either.