Home > Backend Development > Python Tutorial > Detailed explanation of uploading avatar to flask

Detailed explanation of uploading avatar to flask

零下一度
Release: 2017-07-17 14:00:07
Original
1639 people have browsed it

The book Flask Web Development is basically finished, and I still need to review it later, but the blog I made always feels a bit crude, so I am using my brain to develop new functions

I thought of the most basic functions today , custom avatar

Such a function is designed into two basic function blocks

1: How to upload and save files

2: How to upload it in the user model Linking the avatar photo to the user

For the second question, after consulting online discussions, I came up with basically two methods.

The first is to convert the image into binary Store in the database

The second method is to store the image directly in the file system


Upload the avatar, I thought for a long time, just upload the file. In fact, There is a path, the database stores this path, and then displays it to the front end. Don’t say anything, let’s see how it is implemented.

The database settings are as follows

user_image=db.Column(db.String(252),nullable=True)
Copy after login

form form design:

avatar=FileField('头像')
Copy after login

Backend implementation code

 avatar=request.files['avatar']
 fanme=avatar.filename
 upfile=os.getcwd()+('/app/static/avatar/')
 ALLOWER_EXIT=['pang','jpg','jpeg','jig']
flag='.' in fanme and fanme.split('.')[1] in ALLOWER_EXITif not flag:     return render_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)
Copy after login

The storage path is

/static/avatar/,支持格式 pang、jpg、jpeg等格式,这个可以根据自己的需求进行设置。 
存储后会在数据库存储一个路径
最后实现后数据库
Copy after login

The next is the front-end display

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

Let me explain here, the code for uploading avatars in the backend here reports an error on Windows, saying that the file does not exist. I tried to modify it, but the path name should not be too long,

The path name can be short, but an error will be reported if it is long. Windows development has many drawbacks, but there is no problem in implementing it on Ubuntu

The above is the detailed content of Detailed explanation of uploading avatar to flask. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template