[英]Flask-Classy url_for issue (builderror)
I'm having troubles embedding an url_for in my templates (or defining in my views) when accessing Flask-Classy view methods. 访问Flask-Classy视图方法时,在将url_for嵌入模板(或在视图中定义)时遇到麻烦。
/app/routes.py /app/routes.py
class BaseView(FlaskView):
route_base '/'
@route('index', endpoint('index')
def index():
return render_template('index.html')
def resetapp():
db.drop_all()
return redirect(url_for('BaseView:index'))
/app/crm/accounts/routes.py /app/crm/accounts/routes.py
class AccountView(FlaskView):
route_base '/crm/account/'
@route('create', endpoint='create')
def create():
return render_template('path/to/create.html')
now inside the 'index.html', i have the following 现在在“ index.html”中,我有以下内容
But I get the following error: werkzeug.routing.BuildError 但是我收到以下错误:werkzeug.routing.BuildError
BuildError: ('AccountView.create', {}, None)
BuildError :(“ AccountView.create”,{},无)
If you look at the first route, there is resetapp
which uses the url_for referencing itself as BaseView:index - this works! 如果您看第一条路线,那儿有一个
resetapp
,它使用url_for引用自己作为BaseView: resetapp
这行得通!
I also tried the same format in the index.html {{ url_for('AccountView:create') }} but same error. 我还在index.html {{url_for('AccountView:create')}}中尝试了相同的格式,但出现了相同的错误。
Any ideas? 有任何想法吗?
It seems that you forget to register the view BaseView.register(app)
,the following is a workable code: 似乎您忘记了注册视图
BaseView.register(app)
,以下是可行的代码:
from flask import Flask,url_for,redirect,render_template
from flask.ext.classy import FlaskView,route
app = Flask(__name__)
class BaseView(FlaskView):
route_base= '/'
@route('index')
def index(self):
print url_for('BaseView:index')
return render_template("index.html")
@route('reset')
def reset(self):
print url_for('BaseView:reset')
return redirect(url_for('BaseView:index'))
BaseView.register(app)
if __name__ == '__main__':
app.run(debug=True)
The problem is that you're overring the endpoints in the route decorator, but still trying to access them from the default endpoints. 问题是您要覆盖路由装饰器中的端点,但仍尝试从默认端点访问它们。 Also
index
is a special method and does not require a route decorator if you want it to map to the root of your FlaskView
. 同样,
index
是一种特殊的方法,如果您希望它映射到FlaskView
的根,则不需要路由装饰器。 (you also forgot the self
parameters!) Try changing your code to this: (您也忘记了
self
参数!)尝试将代码更改为此:
class BaseView(FlaskView):
route_base '/'
def index(self):
return render_template('index.html')
def resetapp(self):
db.drop_all()
return redirect(url_for('BaseView:index'))
now url_for('BaseView:index')
will return "/"
现在
url_for('BaseView:index')
将返回"/"
OK, it's taken a while - but it turns out I lead you on a wild goose chase. 好的,花了一段时间-但事实证明,我带领您进行了野鹅追逐。 The problem was sort of what one person suggested, but the main problem was to do with a route that had arguments...
问题有点像一个人的建议,但主要问题是与一条有争论的路线有关的……
For those who would like to know how to do this. 对于那些想知道如何做的人。 Here is the answer for Flask-Classy url_for()
这是Flask-Classy的答案url_for()
@route('update/<client_id>', methods=['POST','GET'])
def update(self, client_id=None):
if client_id is None:
return redirect(url_for('ClientView:index'))
In your template: 在您的模板中:
{{ url_for('YourView:update', client_id=some_var_value, _method='GET') }}
Here are the things you cannot do - to use Flask-Classy properly. 这是您不能做的事情-正确使用Flask-Classy。
I also specifically supply the method - but this is unnecessary. 我还专门提供了该方法-但这是不必要的。
For point 3 - this is because when there is more than 1 route - how does it know which one to use? 对于第3点-这是因为当有多条路线时-如何知道要使用哪一条? Simple, but this was what I was chasing myself around in circles for.
很简单,但这就是我一直在兜圈子。 Doing everything except removing the additional routes.
除了删除其他路由,请执行所有操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.