Ok, I know there is many question same to mine, but I didn't find any answer to solve my problem.I am newbie to Django, so please keep in mind.
I need to do a from which will upload a file:
Here is my upload.py(which is a views.py)
from django.http import HttpResponse
def upload(request)
viewfile = HttpResponse()
viewfile.write('''
<html>
<body>
<form action="/upload_done/" method="POST" enctype="multipart/form-data" {% csrf_token %}>
<label for="file">Filename:</label>
<input type="file" name="up_file" >
<br />
<input type="submit" name="submit" value="Submit" >
</form>
return HttpResponse(viewfile)
Now My upload_done.py:
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponse
import tempfile
import shutil
def upload_done(request):
viewfile = HttpResponse()
#####First I Tried this
Up_file = request.FILES['up_file']
""" It gives multivalue error('' 'up_file' '')
# then I change my upload.py input type file to text & try to collect information
# and change here Up_file = request.FILES['up_file'] to this
Up_file = request.POST['up_file']
Now I am getting 403 csrf error From Django doc, Iam not able to understand how templates(only able to understand file.html should be same name as views.py's funcution name) works.
Please assist me how to use post method & how to upload a file.Thanks In advance...
I have tried like this also(write in /home/user/myproject/template/upload_form.html)(Note: template dirctory works properly)
<html>
<body>
<form action="/upload_done/" method="POST" enctype="multipart/form-data" {% csrf_token %}>
<label for="file">Filename:</label>
<input type="file" name="up_file" >
<br />
<input type="submit" name="submit" value="Submit" >
</form>
& In above 2nd Views.py(i.e. upload_form.py) replace 'Up_file = request.FILES['up_file']' to this
if request.method == 'POST':
return render(request, 'upload_form.html',{})
But getting error (must return HttpResponse)