简体   繁体   English

Flask视图引发“ AttributeError:'function'对象没有属性”

[英]Flask view raises “AttributeError: 'function' object has no attribute”

app.py defines a sectors view that use a Sectors class defined in sectors.py . app.py限定sectors查看使用一个Sectors中定义的类sectors.py When I access the view, I get an AttributeError : 访问视图时,出现AttributeError

    sector = sectors.Sectors()
AttributeError: 'function' object has no attribute 'Sectors'
import sectors

@app.route("/sectors")
def sectors():
    sector = sectors.Sectors()
    return render_template('sectors.html', sector=sector)  

I imported sectors , so it should be a module, not a function, and it does have Sectors defined. 我导入了sectors ,所以它应该是一个模块,而不是一个函数,并且确实定义了Sectors Why isn't this working? 为什么这不起作用?

Your view function has the same name as a name you imported earlier. 您的视图函数具有与先前导入的名称相同的名称。 Since the view function is defined after the import in the file, it is what the name points at. 由于视图功能是在文件导入后定义的,因此名称即指向该功能。

Either alias the import: 可以为导入别名:

import sectors as sectors_mod

@app.route("/sectors")
def sectors():
    sectors_mod.Sectors()
    ...

Or change the name of the function. 或更改函数的名称。 You can still keep the endpoint name as "sectors" for use with url_for . 您仍然可以将端点名称保留为"sectors"以便与url_for一起使用。

import sectors

@app.route("/sectors", endpoint="sectors")
def sectors_view():
    sectors.Sectors()
    ...

In either case, the names of the import and function are distinct, and the endpoint name remains "sectors" in both cases, so url_for("sectors") still works. 在这两种情况下,导入和函数的名称都是不同的,并且端点名称在两种情况下均保持为"sectors" ,因此url_for("sectors")仍然有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在蓝图上注册路由会引发AttributeError:'function'对象没有属性'route' - Registering route on blueprint raises AttributeError: 'function' object has no attribute 'route' AttributeError:“函数”对象没有属性“as_view” - AttributeError: 'function' object has no attribute 'as_view' flask-mail AttributeError:“函数”对象没有属性“发送” - flask-mail AttributeError: 'function' object has no attribute 'send' AttributeError: 'function' object 在 Flask web 应用程序中没有属性 'make' - AttributeError: 'function' object has no attribute 'make' in Flask web app Flask-sqlalchemy AttributeError: 'function' object 没有属性 'query' - Flask-sqlalchemy AttributeError: 'function' object has no attribute 'query' .endswith() 方法引发异常“AttributeError: 'str' object has no attribute 'value'” - .endswith() method raises exception "AttributeError: 'str' object has no attribute 'value'" suds 3` __inject`引发AttributeError:'NoneType'对象没有属性'promotePrefixes' - suds 3 `__inject` raises AttributeError: 'NoneType' object has no attribute 'promotePrefixes' Flask-AttributeError:“ _ AppCtxGlobals”对象没有属性“ db” - Flask - AttributeError: '_AppCtxGlobals' object has no attribute 'db' Flask-AttributeError:“模块”对象没有属性“ items” - Flask - AttributeError: 'module' object has no attribute 'items' Flask AttributeError: 'LoginForm' 对象没有属性 'userEmail' - Flask AttributeError: 'LoginForm' object has no attribute 'userEmail'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM