例程albumy裁剪头像:未上传头像时使用原始头像裁剪报错 TypeError: join()……

在原始随机默认头像上裁剪保存会提示如下错误:

builtins.TypeError
TypeError: join() argument must be str or bytes, not 'NoneType'

Traceback (most recent call last)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_login\utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "D:\FlaskProjects\FlaskHTTP\myalbumy\myalbumy\decorators.py", line 21, in decorated_function
return func(*args, **kwargs)
File "D:\FlaskProjects\FlaskHTTP\myalbumy\myalbumy\blueprints\user.py", line 178, in crop_avatar
filenames = avatars.crop_avatar(current_user.avatar_raw, x, y, w, h)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_avatars\__init__.py", line 312, in crop_avatar
path = os.path.join(current_app.config['AVATARS_SAVE_PATH'], filename)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\ntpath.py", line 115, in join
genericpath._check_arg_types('join', path, *paths)
File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\genericpath.py", line 149, in _check_arg_types
Open an interactive python shell in this frame(funcname, s.__class__.__name__)) from None

原因应该是current_user.avartar_raw为None,该如何修改?

配置 AVATARS_SAVE_PATH 定义了吗?

AVATARS_SAVE_PATH = os.path.join(ALBUMY_UPLOAD_PATH, 'avatars')   # 头像存储路径

配置了

打印 current_user.avatar_raw 显示什么?

None

@user_bp.route('/settings/avatar/crop', methods=['POST'])
@login_required
@confirm_required
def crop_avatar():
    """
    视图:裁剪头像
    """
    form = CropAvatarForm()
    print(current_user.avatar_raw)
    if form.validate_on_submit():
        x = form.x.data
        y = form.y.data
        w = form.w.data
        h = form.h.data
        filenames = avatars.crop_avatar(current_user.avatar_raw, x, y, w, h)

        current_user.avatar_s = filenames[0]
        current_user.avatar_m = filenames[1]
        current_user.avatar_l = filenames[2]
        db.session.commit()
        flash('头像已更改', 'success')
    flash_errors(form)
    return redirect(url_for('.change_avatar'))

结果:

27.0.0.1 - - [03/Mar/2019 11:12:45] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Mar/2019 11:12:45] "GET /static/favicon.ico HTTP/1.1" 304 -
127.0.0.1 - - [03/Mar/2019 11:12:48] "GET /auth/login HTTP/1.1" 200 -
127.0.0.1 - - [03/Mar/2019 11:12:57] "POST /auth/login HTTP/1.1" 302 -
127.0.0.1 - - [03/Mar/2019 11:12:57] "GET /auth/login HTTP/1.1" 302 -
127.0.0.1 - - [03/Mar/2019 11:12:57] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Mar/2019 11:13:02] "GET /user/settings/profile HTTP/1.1" 200 -
127.0.0.1 - - [03/Mar/2019 11:13:27] "GET /user/settings/avatar HTTP/1.1" 200 -
None
127.0.0.1 - - [03/Mar/2019 11:13:35] "POST /user/settings/avatar/crop HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_login\utils.py", line 261, in decorated_view
    return func(*args, **kwargs)
  File "D:\FlaskProjects\FlaskHTTP\myalbumy\myalbumy\decorators.py", line 21, in decorated_function
    return func(*args, **kwargs)
  File "D:\FlaskProjects\FlaskHTTP\myalbumy\myalbumy\blueprints\user.py", line 179, in crop_avatar
    filenames = avatars.crop_avatar(current_user.avatar_raw, x, y, w, h)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_avatars\__init__.py", line 312, in crop_avatar
    path = os.path.join(current_app.config['AVATARS_SAVE_PATH'], filename)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\ntpath.py", line 115, in join
    genericpath._check_arg_types('join', path, *paths)
  File "C:\Users\张启宏\.virtualenvs\FlaskProjects-BArbVR56\lib\genericpath.py", line 149, in _check_arg_types
    (funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'NoneType'

显示出:None

这个是 Flask-Avatars 的 bug,我这两天会修复并发布新版本。

1 个赞

好的,修复后通知一声!

OK

Flask-Avatars 0.2.2 发布了。

1 个赞

可pipenv install Flask-Avatars 安装后,仍然是0.2.1版本

(FlaskProjects-BArbVR56) D:\FlaskProjects>pipenv install flask-avatars
Installing flask-avatars…
Collecting flask-avatars
  Using cached https://files.pythonhosted.org/packages/6f/af/22fa0d17f1c3475ae0d6de9134e101ae134b9d713070bb68b4921fda0ebe/Flask_Avatars-0.2.2-py2.py3-none-any.whl
Requirement already satisfied, skipping upgrade: Pillow in c:\users\张启宏\.virtualenvs\flaskprojects-barbvr56\lib\site-packages (from flask-avatars) (5.4.1)
Requirement already satisfied, skipping upgrade: Flask in c:\users\张启宏\.virtualenvs\flaskprojects-barbvr56\lib\site-packages (from flask-avatars) (1.0.2)
Requirement already satisfied, skipping upgrade: itsdangerous>=0.24 in c:\users\张启宏\.virtualenvs\flaskprojects-barbvr56\lib\site-packages (from Flask->flask-avatars) (1.1.0)
Requirement already satisfied, skipping upgrade: Werkzeug>=0.14 in c:\users\张启宏\.virtualenvs\flaskprojects-barbvr56\lib\site-packages (from Flask->flask-avatars) (0.14.1)
Requirement already satisfied, skipping upgrade: Jinja2>=2.10 in c:\users\张启宏\.virtualenvs\flaskprojects-barbvr56\lib\site-packages (from Flask->flask-avatars) (2.10)
Requirement already satisfied, skipping upgrade: click>=5.1 in c:\users\张启宏\.virtualenvs\flaskprojects-barbvr56\lib\site-packages (from Flask->flask-avatars) (7.0)
Requirement already satisfied, skipping upgrade: MarkupSafe>=0.23 in c:\users\张启宏\.virtualenvs\flaskprojects-barbvr56\lib\site-packages (from Jinja2>=2.10->Flask->flask-avatars) (1.1.0)
Installing collected packages: flask-avatars
  Found existing installation: Flask-Avatars 0.2.1
    Uninstalling Flask-Avatars-0.2.1:
      Successfully uninstalled Flask-Avatars-0.2.1
Successfully installed flask-avatars-0.2.2

Adding flask-avatars to Pipfile's [packages]…
Installing dependencies from Pipfile.lock (2741a3)…
  ================================ 30/30 - 00:00:03
Traceback (most recent call last):
  File "d:\phthon37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "d:\phthon37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Phthon37\Scripts\pipenv.exe\__main__.py", line 9, in <module>
  File "d:\phthon37\lib\site-packages\pipenv\vendor\click\core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "d:\phthon37\lib\site-packages\pipenv\vendor\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "d:\phthon37\lib\site-packages\pipenv\vendor\click\core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "d:\phthon37\lib\site-packages\pipenv\vendor\click\core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "d:\phthon37\lib\site-packages\pipenv\vendor\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "d:\phthon37\lib\site-packages\pipenv\vendor\click\decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "d:\phthon37\lib\site-packages\pipenv\vendor\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "d:\phthon37\lib\site-packages\pipenv\vendor\click\decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "d:\phthon37\lib\site-packages\pipenv\cli\command.py", line 249, in install
    editable_packages=state.installstate.editables,
  File "d:\phthon37\lib\site-packages\pipenv\core.py", line 1978, in do_install
    requirements_directory.cleanup()
  File "d:\phthon37\lib\site-packages\pipenv\vendor\vistir\compat.py", line 123, in cleanup
    rmtree(self.name)
  File "d:\phthon37\lib\site-packages\pipenv\vendor\vistir\path.py", line 248, in rmtree
    directory, ignore_errors=ignore_errors, onerror=handle_remove_readonly
  File "d:\phthon37\lib\shutil.py", line 507, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "d:\phthon37\lib\shutil.py", line 368, in _rmtree_unsafe
    onerror(os.scandir, path, sys.exc_info())
  File "d:\phthon37\lib\shutil.py", line 365, in _rmtree_unsafe
    with os.scandir(path) as scandir_it:
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: b'C:\\Users\\\\AppData\\Local\\Temp\\pipenv-09bv3oaj-requirements'

(FlaskProjects-BArbVR56) D:\FlaskProjects>pip list
Package            Version
------------------ -------
alembic            1.0.5
argh               0.26.2
blinker            1.4
Bootstrap-Flask    1.0.9
Click              7.0
Faker              1.0.2
Flask              1.0.2
Flask-Avatars      0.2.1
Flask-CKEditor     0.4.3
Flask-DebugToolbar 0.10.1
Flask-Dropzone     1.5.3
Flask-Login        0.4.1
Flask-Mail         0.9.1
Flask-Migrate      2.3.1
Flask-Moment       0.6.0
Flask-SQLAlchemy   2.3.2
flask-whooshee     0.6.0
Flask-WTF          0.14.2
itsdangerous       1.1.0
Jinja2             2.10
Mako               1.0.7
MarkupSafe         1.1.0
pathtools          0.1.2
Pillow             5.4.1
pip                18.1
python-dateutil    2.8.0
python-dotenv      0.10.1
python-editor      1.0.3
PyYAML             3.13
setuptools         40.6.3
six                1.12.0
SQLAlchemy         1.2.18
text-unidecode     1.2
Unidecode          1.0.23
watchdog           0.9.0
Werkzeug           0.14.1
wheel              0.32.3
Whoosh             2.7.4
WTForms            2.2.1

(FlaskProjects-BArbVR56) D:\FlaskProjects>

pip install flask-avatars可以更新版本

李老师:可解决吗?

按照文档应该使用 pipenv update flask-avatars,不过 Pipenv 目前的实现和文档不符,并且包含争议。这个命令实际会更新所有的依赖

2 个赞

:+1::+1::+1::+1:完美解决!