Reputation: 155
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.
Upvotes: 0
Views: 42
Reputation: 897
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 (i.e. via POST request). Check out the example here (it's short and sweet):
http://code.activestate.com/recipes/146306/
Upvotes: 1