There are many different web frameworks under Python, and Django is one of the most iconic of the heavyweights. 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 Form class to deal with the form , through the instantiation of processing and rendering in the template , you can easily complete the form requirements , the use of django form processing , can help us save a lot of work , such as validation can not be null , or to comply with a certain mode of input to be effective , these are very convenient to deal with , do not have to write their own code to verify the data correctness of the form alone . form of data correctness, so more commonly used in the development of Form provides a lot of form fields, such as date, text type, etc., if you are familiar with the basic html, learning will be very easy to get started, so today we do not intend to explain each form field by field, today, only the uploading of the form file, because this type is more specific, requires a little special processing, let's Create a simple one example:
First we create a simple form using Form:
class UserForm(): username = (required=False) headImg = () class UserForm(): username = (required=False) headImg = ()
This form has just 2 fields, asking the user to enter a username and upload a file or image.
Next we put into the template to render, this time you can see a basic form, the view function is as follows:
def register(request): if == "POST": uf = UserForm(, ) if uf.is_valid(): # Put code to upload files return HttpResponse('ok') else: uf = UserForm() return render(request, '', {'uf': uf}) def register(request): if == "POST": uf = UserForm(, ) if uf.is_valid(): # Put code to upload files return HttpResponse('ok') else: uf = UserForm() return render(request, '', {'uf': uf})
This function determines whether the user's POST request, if so and verify that it is valid, and then return OK, in the verification of the correct and return OK in the middle of the upload file to put our code, because only the file upload can be returned to OK, we will say in a moment, if it is a GET request, it directly displays an empty form, so that the user input.
Processing an uploaded file is to generate a file to the server and write the contents of the uploaded file to the new file, so its basic function is like this, receive the uploaded file object as a parameter, and then open a file locally, read the file from the uploaded file, and write it to the new file, the code is as follows:
def handle_uploaded_file(f): with open('/server/testform/upload/' + , 'wb+') as destination: for chunk in (): (chunk) def handle_uploaded_file(f): with open('/server/testform/upload/' + , 'wb+') as destination: for chunk in (): (chunk)
With this handler for uploading files, we can further refine our view function to end up with the following code:
def register(request): if == "POST": uf = UserForm(, ) if uf.is_valid(): handle_uploaded_file(['headImg']) return HttpResponse('ok') else: uf = UserForm() return render(request, '', {'uf': uf}) def register(request): if == "POST": uf = UserForm(, ) if uf.is_valid(): handle_uploaded_file(['headImg']) return HttpResponse('ok') else: uf = UserForm() return render(request, '', {'uf': uf})
This completes the upload of a file, over.
The above is a small introduction to django1.8 using the form to upload files, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. Here also thank you very much for your support of my website!