Flask Upload Avatar

Source: Internet
Author: User

Upload Avatar, I feel for a long time, is to upload the file Bai Actually, there is a path, database storage this path, and then show to the front end, nothing to say, see how to achieve.

The database settings are as follows

User_image=db. Column (db. String (252), nullable=true)

Form form Design:

Avatar=filefield (' avatar ')

Back-end implementation code

avatar=request.files['Avatar'] Fanme=Avatar.filename upfile=OS.GETCWD () + ('/app/static/avatar/') Allower_exit=['Pang','jpg','JPEG','Jig']flag='.' inchFanme andFanme.split ('.') [1]inchAllower_exitif  notflag:returnRender_template ('editperson.html', form=form) Avatar.save ('{}{}{}'. Format (upfile,user.username,fanme)) User.user_image='/static/avatar/{}{}'. Format (user.username,fanme) db.session.add (user)

The storage path is


A path is stored in the database after storage
After the final implementation of the database

Next is the front-end display

{% if username.user_image%} < img    src = "{{username.user_image}}" style = "height:80px;" > {%else%} <  src= "/static/img/0.jpg"  style= "height:70px;width:80px" > {%endif%}

Here, the back end of the implementation of the uploaded Avatar code on Windows return error, said the file does not exist, I tried to modify, that is, the path name is not too long,

The path name is short, the error will be long. Windows development is a disadvantage, there is no problem in Ubuntu implementation

Project Address Https://github.com/liwanlei/flask_blog

Flask Upload Avatar

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.