烧瓶学习问题
为了定制对匿名用户的要求,我在模型中设置了一个类:
class MyAnonymousUser(AnonymousUserMixin):
def can(self, permissions):
return False
def is_administrator(self):
return False
login_manager.anonymous_user = MyAnonymousUser
烧瓶运行遇到错误:'AnonymousUserMixin'
对象在视图中没有属性'can'
,
@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'))
我不明白为什么MyAnonymousUser没有将current_user赋予属性。
这是我的origin code
谢谢你的帮助
参考方案
似乎您假设通过将登录管理器分配给您的类,就可以继承创建的方法。这不是代码中发生的事情。您实际上正在做的是覆盖已经提供的login_manager.anonymous_user的功能。
Check the documentation here
实际上,应该在包含用户的模型中定义函数。似乎您正在尝试检查用户是否为匿名用户。因此,以下是我对编码的建议。
UserModel(db.Model, UserMixin):
def can(self, permissions):
return False
def is_administrator(self):
return False
@main.route('/', methods=['GET', 'POST'])
def index():
form = PostForm()
if current_user.is_active and form.validate_on_submit():
if current_user.can(Permission.WRITE_ARTICLES):
post = Post(body=form.body.data,
author=current_user._get_current_object())
db.session.add(post)
db.session.commit()
return redirect(url_for('.index'))
AttributeError:'NoneType'对象没有属性'traverse' - python我目前正在构建Flask应用程序,并且正在尝试在其中加载Plotly Dash应用程序。我能够从下面的stackoverflow留言板上得到这些信息,但是我认为我做的事情不正确(可能忽略了某些事情?)Running a Dash app within a Flask app这里的任何帮助将不胜感激这是我的server.py代码:from flask impo…
Qt5:AttributeError:'模块'对象没有属性'QApplication' - python系统:15.10(Wily Werewolf)x64码:# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'test.ui' # # Created by: PyQt5 UI code generator 5.5.1 # # WARNI…
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。