Submit a form to Django

Source: Internet
Author: User

Python's Django Framework, made a front-end page, which has always been asked to submit the form, for the moment to summarize the process, there is no wrong place please correct me:

1. Write the form on the HTML page:

<form enctype= "Multipart/form-data" method= "POST" action= "{% url ' upload '%}" >

{% Csrf_token%}
<input type= "File" name= "Your_file"/>
<button type= "Submit" >Submit</button>
</form>

where enctype= "Multipart/form-data" is required to ensure the correct encoding of an anonymous upload file (enctype= "Multipart/form-data" in the form means setting the MIME encoding of the form. By default, this encoding format is application/x-www-form-urlencoded, cannot be used for file upload, only use the Multipart/form-data to complete the transfer of file data, do the following.
Enctype= "Multipart/form-data" is to upload binary data; The value of input in the form is passed in 2 binary, while action= "{% url ' upload '%}" refers to the time required to submit the form data to a location in the background, {% Csrf_token%}, Otherwise, the submission form will return a 403 error: CSRF verification failed. Request aborted.

2. Add code to the urls.py in the application in the Django framework to ensure that the page can be found:

URL (r ' ^upload/$ ', views.upload, name= ' upload '),

Where the first variable is a regular expression and the second is for the handler that is submitted to the page

3. Add the upload function to the views.py in the application in the Django Framework, which is the views.upload in the second part to process the submitted form:

def upload (Request):

Return ...

At this point, the form functionality is basically implemented.

Submit a form to Django

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.