django1.8 USES a form to upload files

  • 2020-05-17 05:45:48
  • OfStack

There are many different Web frameworks under Python. Django is the most representative of the heavyweights. Many successful websites and APP are based on Django.

Django is an open source Web application framework written by Python.

We can use in django Form class to handle the form, by instantiating processing and rendering in the template, you can easily complete the form of demand, django form processing method, can help us save a lot of work, such as verification can't be empty, or to conform to a certain mode of the input is valid, these very convenient to handle and don't separate again write code to validate form data is correct, so the more commonly used in the development, Form provides a lot of form fields, such as date, text type and so on, if you are familiar with basic html, It will be very easy to learn, so today we are not going to explain the fields of each form 1 by 1. We will only talk about the upload of the form file, because this type is special and requires 1 point of special processing. Let's create a simple example:

First we create a simple form using Form:


class UserForm(forms.Form):
username = forms.CharField(required=False)
headImg = forms.FileField()
class UserForm(forms.Form):
username = forms.CharField(required=False)
headImg = forms.FileField()

This form has two fields and asks the user to enter a user name and upload a file or image.

Next, we put it into the template to render. At this point, we can see a basic form. The view function is as follows:


def register(request):
if request.method == "POST":
uf = UserForm(request.POST, request.FILES)
if uf.is_valid():
# The code to put the uploaded file 
return HttpResponse('ok')
else:
uf = UserForm()
return render(request, 'register.html', {'uf': uf})
def register(request):
if request.method == "POST":
uf = UserForm(request.POST, request.FILES)
if uf.is_valid():
# The code to put the uploaded file 
return HttpResponse('ok')
else:
uf = UserForm()
return render(request, 'register.html', {'uf': uf})

This function to determine whether a user is POST request, if it is and validation is effective, and then return to OK, in the middle of the verify correct and return OK our code to upload files, because only returning to OK file upload successfully, we will say, 1 if it is GET request, directly display a blank form, let the user input.

To process the uploaded file is to generate a file on the server and write the uploaded file content to the new file, so its basic function is like this: accept the uploaded file object as a parameter, and then open a file locally, read the file from the uploaded file, and write to the new file, the code is as follows:


def handle_uploaded_file(f):
with open('/server/testform/upload/' + f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
def handle_uploaded_file(f):
with open('/server/testform/upload/' + f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)

With this file upload function, we can further improve our view function. The final code is as follows:


def register(request):
if request.method == "POST":
uf = UserForm(request.POST, request.FILES)
if uf.is_valid():
handle_uploaded_file(request.FILES['headImg'])
return HttpResponse('ok')
else:
uf = UserForm()
return render(request, 'register.html', {'uf': uf})
def register(request):
if request.method == "POST":
uf = UserForm(request.POST, request.FILES)
if uf.is_valid():
handle_uploaded_file(request.FILES['headImg'])
return HttpResponse('ok')
else:
uf = UserForm()
return render(request, 'register.html', {'uf': uf})

This completes the upload of 1 file. Over.


Related articles: