Friends this is the continuation from its previous post so i kindly recommend you to read that post i have made this very simple even a complete beginner can learn it and if you are beginner in python then you need to learn python i have made that blog also for that click this link
HANDLING UPLOADED files
In the previous post we have learn to bind the request.FILES into the forms constructor now here we will learn common methods to handle the uploaded file as for example an uploaded file can be
def handle_uploaded(f) :
with open ('some/file/name.txt','wb+') as destination :
for chunk in f.chunks():
destination.write(chunk)
with open ('some/file/name.txt','wb+') as destination :
for chunk in f.chunks():
destination.write(chunk)
Looping over UploadedFile.chunks() instead of using read() ensure that large file don't overwhelm your systems memory
HANDLING UPLOADED FILE WITH MODEL
If you are saving file on a Model with a FileField using a ModelForm the file object will be saved to the location specified by the upload_to argument of the corresponding FileField when calling form.save
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import ModelFormWithFileField
def upload_file (request):
if request.method==POST:
form = ModelFormWithFileField(request.POST,request.FILES)
if form.is_valid():
form.save
return HttpResponseRedirect('/success/url/')
else:
form = ModelFormWithFileField()
return render (request,'upload.html',{'form':form})
from .forms import ModelFormWithFileField
def upload_file (request):
if request.method==POST:
form = ModelFormWithFileField(request.POST,request.FILES)
if form.is_valid():
form.save
return HttpResponseRedirect('/success/url/')
else:
form = ModelFormWithFileField()
return render (request,'upload.html',{'form':form})
If you are constructing an object manually you can simply assign the file object from request.FILES to the file field in the model
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .form import UploadFileForm
from .model import ModelWithFileField
def upload_file(request):
if request.method == POST:
form = UploadFileForm (request.POST,request.FILES)
if form.is_valid():
instance = ModelWithFileField(file_field= request.FILES['file'])
instance.save()
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render (request ,'upload.html',{'form':form})
from django.shortcuts import render
from .form import UploadFileForm
from .model import ModelWithFileField
def upload_file(request):
if request.method == POST:
form = UploadFileForm (request.POST,request.FILES)
if form.is_valid():
instance = ModelWithFileField(file_field= request.FILES['file'])
instance.save()
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render (request ,'upload.html',{'form':form})
Uploading Multiple Files
the multiple files can be uploaded with the one form field . Set the multiple HTML attribute of the fields Widget
from django import forms
class FileFieldForm(form.Form):
file_field = forms.FileField (widget = forms.ClearableFileInput(attrs{'multiple':True}))
Then override the post method of the FormView subclass to handle the multiple file uploads
from django.views.generics.edit import FormView
from .forms import FileFieldForm
class FileFieldView(FormView):
form_class = FileFieldForm()
template_name = 'upload.html' #you can replace with your template
success_url = '...' #replace with your url or reverse()
def post(self,request,*args,**kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getList('file_field')
if form.is_valid():
for f in files
.....#do something with each file
return self.form_valid (form)
else:
return self.form_invalid(form)
from .forms import FileFieldForm
class FileFieldView(FormView):
form_class = FileFieldForm()
template_name = 'upload.html' #you can replace with your template
success_url = '...' #replace with your url or reverse()
def post(self,request,*args,**kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getList('file_field')
if form.is_valid():
for f in files
.....#do something with each file
return self.form_valid (form)
else:
return self.form_invalid(form)
That's all for this post guys now you have the basic knowledge to start learning the advanced lessons on django more advanced lessons will be included in future posts
hi
ReplyDeletehow to fix bugs in django?
you can check out this link for that or specify more details about your question
Deletehttps://code.djangoproject.com/#Djangosbugtrackerandwiki