I have a simple view which receives files form users. I've tested it with my browser and it works okay.
On the other hand, I have a python and tested it against requestb.in and I can see the multipart/form-data and a http response code 200. So, both my view in receiving and storing files and the script in uploading multipart data work fine.
My problem being: When I use my script to upload a file to the view, even though I get http status code 200, I see no files being stored on the disk while in case of using the browser I do. What would be the problem you think? Here are my model, view and script:
models.py:
class FileUploads(models.Model):
uploads = models.FileField()
forms.py:
from django import forms
from .models import FileUploads
class UserProfileForm(forms.ModelForm):
class Meta:
model = FileUploads
fields = ['uploads']
views.py:
@csrf_exempt
def upper(request):
form = UserProfileForm(request.POST or None, request.FILES or None)
context = {'title': 'welcome', 'form': form}
if form.is_valid():
form.save()
context = {'form': form, 'title': 'thanks'}'
return render(request, 'upform.html', context)
Python script up.py:
import requests
f = open('C:\\Users\\Amir\\Desktop\\snow.jpg', 'rb')
urls='http://127.0.0.1:8000/upper'
r=requests.post(urls, files= {'a':f})
print(r.status_code)
files= {'uploads':f}?@csrf_exemptdecorator from the view