Uploading avatars, I thought for a long time, it was just about uploading files. In fact, there is a path, the database stores this path, and then displays it to the front end, without saying anything, let’s see how it is implemented.
The database settings are as follows
user_image=db.Column(db.String(252),nullable=True)
form form design:
avatar=FileField('头像')
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)
The storage path is
/static/avatar/,支持格式 pang、jpg、jpeg等格式,这个可以根据自己的需求进行设置。 存储后会在数据库存储一个路径 最后实现后数据库
The next is the front-end display
{% if username.user_image%}<img src="/static/imghwm/default1.png" data-src="{{username.user_image}}" class="lazy" alt="Detailed explanation of flask upload avatar example" >{%else%}<img src="/static/imghwm/default1.png" data-src="/static/img/0.jpg" class="lazy" alt="Detailed explanation of flask upload avatar example" >{%endif%}
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 flask upload avatar example. For more information, please follow other related articles on the PHP Chinese website!

随着移动互联网和Web技术的迅速发展,越来越多的应用需要提供流畅、快速的用户体验。传统的多页面应用已经无法满足这些需求,而单页面应用(SPA)则成为了解决方案之一。那么,如何快速实现单页面应用呢?本文将介绍如何利用Flask和Vue.js来构建SPA。Flask是一个使用Python语言编写的轻量级Web应用框架,它的优点是灵活、易扩

一、日志输出到文件使用模块:logging可以生成自定义等级日志,可以输出日志到指定路径日志等级:debug(调试日志)=5){clearTimeout(time)//如果连续10次获取的都是空日志清除定时任务}return}if(data.log_type==2){//如果获取到新日志for(i=0;i

在第一部分介绍了基本的Flask和IntellijIDEA集成、项目和虚拟环境的设置、依赖安装等方面的内容。接下来我们将继续探讨更多的Pythonweb应用程序开发技巧,构建更高效的工作环境:使用FlaskBlueprintsFlaskBlueprints允许您组织应用程序代码以便于管理和维护。Blueprint是一个Python模块,能够包

Flask-RESTful和Swagger:Pythonweb应用程序中构建RESTfulAPI的最佳实践(第二部分)在上一篇文章中,我们探讨了如何使用Flask-RESTful和Swagger来构建RESTfulAPI的最佳实践。我们介绍了Flask-RESTful框架的基础知识,并展示了如何使用Swagger来构建RESTfulAPI的文档。本

Flask和SublimeText集成:Pythonweb应用程序开发技巧(第六部分)SublimeText和Flask都是Pythonweb应用程序开发中的重要工具。然而,如何将二者集成起来,使得开发过程更加高效呢?本文将介绍一些SublimeText的插件和配置技巧,帮助你更方便地开发Flask应用程序。一、安装SublimeText插件F

Flask和Eclipse集成:Pythonweb应用程序开发技巧(第三部分)在前两篇文章中,我们介绍了如何将Flask与Eclipse集成,以及如何创建Flask应用程序。在本文中,我们将继续探讨如何开发和调试Flask应用程序,以及如何管理数据库。一、开发和调试Flask应用程序创建和运行Flask应用程序在Eclipse的ProjectExplo

一、概述Flask是一个轻量级的PythonWeb框架,支持Jinja2模板引擎。Jinja2是一个流行的Python模板引擎,它可以使用Flask来创建动态Web应用程序。web页面一般需要html、css和js,可能最开始学习pythonweb的时候可能这样写:fromflaskimportFlaskapp=Flask(__name__)@app.route('/')defhello():return'hellohelloworld!!!&am

Flask-Security:在Pythonweb应用程序中添加用户身份验证和密码加密随着互联网的不断发展,越来越多的应用程序需要用户身份验证和密码加密来保护用户数据的安全性。而在Python语言中,有一个非常流行的Web框架——Flask。Flask-Security是基于Flask框架的一个扩展库,它可以帮助开发人员在Pythonweb应用程序中轻


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
