Requirement:
One of the functions of my web app is to upload a file from the Django server to a remote server. This file is not a part of the incoming request to the Django server. It is a test file already existing in a MEDIA or a STATIC location in Django.
Feasibility Test:
To test if this is possible, I'm trying to read a file from my Django application and spit its content out as HttpResponse.
Problem:
def test(request):
text = open(os.path.join(settings.MEDIA_ROOT, '/media/file.txt', 'r').read()
return HttpResponse(text)
File "/<LOCATION>/views.py", line 27
return HttpResponse(text)
^
The above code gives me a Syntax error.
Questions:
Any help would be greatly appreciated.
Answer 1: You forgot the closing paranthesis to join on the line above.
It should be:
open(os.path.join(settings.MEDIA_ROOT, '/media/file.txt'), 'r').read()
Answer 2: You can use httplib to upload a file (ie via POST request). Check out the example here (it's short and sweet):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.