# URL: /api/portrait/upload
# 上传头像
class ApiPortraitUploadHandler(BaseHandler):
@login_required()
def post(self):
# 获取当前图片的名称
imgN = self.get_argument('imgN')
imgE = self.get_argument('imgE')
imgFile = os.path.realpath(os.path.join(os.path.dirname(__file__),
"../static/uploads/portrait/tmp",imgN)+imgE)
if os.path.exists(imgFile):
os.remove(imgFile) #删除用户切换的大图片
分类 "Python" 下的文章
问题:python的字典列表如何按基个key的指定顺序排序?
解决:使用sort或sorted
方法:
users_list = [{'id': a.id, 'name': a.name} for a in users]
user_ids = [32, 12, 43, 11]
users_list.sort(key=lambda x:user_ids.index(x['id']))
或者
sorted(users_list, key=lambda x: user_ids.index(x["id"]))
参考:ttps://www.v2ex.com/t/544116
问题:由于数据库中html文本中标签有部分没有闭合,导致使用htmltopdf转换时失败,如何将未闭合的标签进行补全?
解决:使用BeautifulSoup的prettify
方法:
from bs4 import BeautifulSoup as bs
a = '<p><a href="http://mantutu.com">mantutu</a><span>蛮兔兔</span>'
bs(a, 'lxml').prettify()
输出如下:
'<html>\n <body>\n <p>\n <a href="http://www.mantutu.com">\n mantutu\n
</a>\n <span>\n 蛮兔兔\n </span>\n </p>\n </body>\n</html>'
bs(a, 'html.parser').prettify()
输出如下:
'<p>\n <a href="http://www.mantutu.com">\n mantutu\n </a>\n <span>\n
蛮兔兔\n </span>\n</p>'
所以根据需要,使用html.parser
参考:ttps://www.cnblogs.com/forward-wang/p/5978485.html
问题:sqlalchemy.exc.CompileError: Can't resolve label reference for ORDER BY / GROUP BY. Textual SQL expression 'status desc' should be explicitly declared as text('status desc')
解决:新版本sqlalchemy不能直接使用order_by("status desc")了,需要使用User.status.desc()
方法:
users = self.db.query(User).order_by(User.status.desc()).all()
对于func方法需要先定义变量,如下
user_num = func.count(User.id).label('num')
users = self.db.query(User.age, user_num).filter(User.classes == 12).order_by(
user_num.desc()).all()
参考:ttps://www.e-learn.cn/content/wangluowenzhang/448355
问题:如何安装win32com模块?ModuleNotFoundError: No module named 'win32com'
解决:安装pywin32模块
方法:
pip install pywin32