How Django takes user-uploaded avatars

Source: Internet
Author: User

In the process of user information management, if you need to modify the user's avatar will need to put the user's uploaded avatar in the database, Django can be defined as a file in the form to automatically save the uploaded files.

For this feature, you can use the form to define an image of a file type, and take this field out to actually be a file in memory:

1, first define a form specifically for the processing of uploaded files

Class Uploadimageform (forms. Modelform):
Class Meta:
Model = UserProfile #继承UserProfile类
Fields = [' image '] #对image字段进行form处理

2. Define the view function that modifies the user's avatar
Class Uploadimageview (Loginrequiremixin,view):
"""
User modified Avatar
"""
Def post (self, request):
Image_form = Uploadimageform (Request. POST, request. FILES, instance= request.user) #这里不同的是第二个参数, only need to pass the request. POST, but it is important that the file upload address is required for the document,
Because the file is placed in different places of the request for input and file uploads, the file is present in the request. Files in this variable, modelform can be stored directly into the database, instance this parameter is an instantiation of the object, is passed Modelform class inside the specified userprofile
Object is Request.user
        If Image_form.is_valid ():  #判断image_form是否可以
Image_form.save ()
Return HttpResponse ("{' Status ': ' Success '}", content_type= ' Application/json ')
Else
Return HttpResponse ("{' Status ': ' Fail ', ' msg ': ' Add error '} ', content_type= ' Application/json ')

 <form class= "Clearfix" id= "Jsavatarform" enctype= "Multipart/form-data"  autocomplete= "Off" method= "post" action= "{% url ' users:image_upload '%}" target= ' Framefile '; 
<label C lass= "Changearea" for= "Avatarup",
<span id= "Avatardiv" class= "pic";

</span& Gt
<span class= "fl upload-inp-box" style= "margin-left:70px;"
<span class= "button btn-green btn-w100" id= "jsavatarbtn" > Modify avatar </span>
<in Put type= "file" name= "image" Id= "Avatarup" class= "js-img-up"/>
</span>
</label>
{% Csrf_token%}
</form>
Submit avatar is through form, this field of form enctype= "Multipart/form-data" must be this type, This will pass the file type to our backend
Method= "POST" action= "{% url ' users:image_upload '%}" These two parameters are also required.
<input type= "file" name= "image" Id= "Avatarup" class= "js-img-up"/> Here the name must be "image", which is important here.



How Django takes user-uploaded avatars

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.