文章内容

2023/5/24 10:14:27,作 者: 黄兵

AttributeError: 'function' object has no attribute 'route'

今天新增一个 Flask 中的蓝图,结果报如下错误:

AttributeError: 'function' object has no attribute 'route'

具体代码如下:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from flask import render_template, g
from . import product
from ..decorators import nmr_decorators
@product.route('/', methods=['GET'])
@nmr_decorators()
def product(product_name):
return render_template('products/products.html', name=g.title, description=g.description, keywords=g.keywords)

出现问题的原因:

发生在路由名称和函数名称相同时就会出现上面的一个错误。

这似乎是 Flask 中的一个错误。

解决方案:

修改 product() 函数名:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from flask import render_template, g
from . import product
from ..decorators import nmr_decorators
@product.route('/', methods=['GET'])
@nmr_decorators()
def get_product(product_name):
return render_template('products/products.html', name=g.title, description=g.description, keywords=g.keywords)


参考资料:

1、Registering route on blueprint raises AttributeError: 'function' object has no attribute 'route'


其它相关推荐:

1、Flask 获取客户端访问uri

2、Flask 视图参数过滤

3、flask url 转换器

4、Flask Bootstrap 使用CDN减轻服务器压力 flask网站优化

5、如何理解flask中的蓝本?


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - AttributeError: 'function' object has no attribute 'route'

分享到:

发表评论

评论列表