Home  >  Article  >  Backend Development  >  Implementation method of uploading files using forms in django1.8

Implementation method of uploading files using forms in django1.8

高洛峰
高洛峰Original
2017-02-22 11:04:261381browse

There are many different web frameworks under Python. Django is the most representative of the heavyweight players. Many successful websites and apps are based on Django.

Django is an open source web application framework written in Python.

In Django we can use the Form class to process forms. By instantiating and rendering in templates, we can easily complete the form requirements. Using Django's form processing method can help us save a lot of work. Work, such as verifying that it cannot be empty, or that the input must match a certain pattern to be valid. These are very convenient to handle. You don’t have to write separate code to verify the correctness of the form data. Therefore, it is more commonly used in development. Form provides a lot of Form fields, such as date, text type, etc., if you are familiar with basic HTML, it will be very easy to learn, so today we are not going to explain each form field one by one. Today we will only talk about uploading form files, because of this The type is quite special and requires some special processing. Let’s create a simple instance:

First we use Form to create a simple 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 only two fields, requiring the user to enter a username and upload a file or image.

Next we put it into the template to render. At this time, you 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():
#放上传文件的代码
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():
#放上传文件的代码
return HttpResponse('ok')
else:
uf = UserForm()
return render(request, 'register.html', {'uf': uf})

This function determines whether the user's request is a POST request. If it is and verifies that it is valid, then it returns OK. Put our upload file code between the verification and return of OK, because only the file upload is successful and OK can be returned. We will do that in a moment. Say, if it is a GET request, an empty form will be displayed directly for the user to enter.

Processing uploaded files is to generate a file on the server and write the contents of the uploaded file to a new file, so its basic function is as follows, receiving the uploaded file object as a parameter, and then opening a file locally file, read the file from the uploaded file and write it into a 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 uploaded file processing function, we 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 a file, complete.

The above is what the editor introduces to you using forms to upload files in django1.8. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more related articles on how to use forms to upload files in Django1.8, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn