V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
sprinkle
V2EX  ›  Python

学习 flask 遇到问题: flask 运行抛出问题, AttributeError: 'AnonymousUserMixin' object has no attribute 'can'

  •  
  •   sprinkle · 2018-11-18 20:24:23 +08:00 · 2146 次点击
    这是一个创建于 1958 天前的主题,其中的信息可能已经有所发展或是发生改变。
    视图函数部分代码(如果删去 current_user.can(Permission.WRITE_ARTICLES) and 部分能运行)
    @main.route('/', methods=['GET', 'POST'])
    def index():
    form = PostForm()
    if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit():
    post = Post(body=form.body.data,
    author=current_user._get_current_object())
    db.session.add(post)
    db.session.commit()
    return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    model 部分代码:

    class AnonymousUser(AnonymousUserMixin):
    def can(self, permissions):
    return False
    def is_administrator(self):
    return False

    login_manager.anonymous_user = AnonymousUser
    2 条回复    2018-11-20 19:46:16 +08:00
    nccer
        1
    nccer  
       2018-11-19 02:54:25 +08:00   ❤️ 1
    我不懂 flask,但看你的报错我猜 current_user 是 AnonymousUserMixin 的实例,而不是 AnonymousUser,但是 current_user 在哪里声明的你又没有贴出来,迷真的很迷。。。。
    sprinkle
        2
    sprinkle  
    OP
       2018-11-20 19:46:16 +08:00
    @nccer current_user 是从 flask_login 模块导入的,搞不懂我添加了 AnonymousUser 类,继承了 AnonymousUserMixin,添加了 can 方法,current_user 应该是 AnonymousUser 的实例,搞不懂。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2745 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 12:24 · PVG 20:24 · LAX 05:24 · JFK 08:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.