0👍
✅
It is actually working right now using Python’s requests module
Ill put the code for all interested…
Django server…
urls.py
...
url(r'^list/$', 'dataports.views.list', name='list'),
...
views.py
@csrf_exempt
def list(request):
# Handle file upload
if request.method == 'POST':
print "upload file----------------------------------------------"
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
print "otra vez.. es valido"
print request.FILES
newdoc = Jobpart(
partfile = request.FILES['docfile']
)
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('dataports.views.list'))
else:
#print "nooooupload file----------------------------------------------"
form = DocumentForm() # A empty, unbound form
# Render list page with the documents and the form
return render_to_response(
'data_templates/list.html',
{'form': form},
context_instance=RequestContext(request)
)
list.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Minimal Django File Upload Example</title>
</head>
<body>
<!-- Upload form. Note enctype attribute! -->
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
<p>
{{ form.docfile }}
</p>
<p><input type="submit" value="Upload" /></p>
</form>
</body>
</html>
Now in the client.
client.py
import requests
url = "http://localhost:8000/list/"
response = requests.post(url,files={'docfile': open('test.txt','rb')})
Now you are able to add some security and stuff.. But it is actually a very simple example..
Thank you all!!!!
2👍
What you want to do is to send a POST
request to the Django app sending a file within it.
You can use python’s standard library httplib
module or the 3rd party requests
module.That last link posted shows how to post a multipart encoded file which is probably what you need.
Hope this helps!
- [Answered ]-Authenticating a Django user with email
- [Answered ]-Django ManyToManyField delete across models
- [Answered ]-Using an abc.ABC class object in a Django template: why Django tries to instantiate it?
- [Answered ]-How to programatically insert HTML in a Django template based on attributes in a model
-1👍
file = request.FILES['file']
load_file = FileSystemStorage()
filename = load_file.save(file.name, file) // saving in local directory and getting filename
data = {'name': name, 'address': address, 'age':age }
fr_data = None
with open(filepath ,'rb') as fr:
fr_data += fr.read()
url = 'http://127.0.0.1:8000/api/'
response = requests.post(url=url, data=data, files= {
'filefiledname': fr_data
}
)
- [Answered ]-Django makemigrations error non-nullable field
- [Answered ]-Cannot set custom parameter with python social auth
- [Answered ]-Django model query tune up
- [Answered ]-Django Wagtail ajax contact form
- [Answered ]-Django login AttributeError: 'AnonymousUser' object has no attribute '_meta'
Source:stackexchange.com