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