python - Decorating Flask Pluggable Views - BuildError -
i'm trying create site containing navigation bar using flask-menu , flask-breadcrumbs. decorator used (flask-menu example):
@app.route('/') @menu.register_menu(app, '.', 'home') def index(): return tmpl_show_menu() #some view
however, want use methodview classes allow more complex operations. template excerpt:
{%- item in current_menu.children -%} <a href="{{ item.url}}">{{ item.text }}</a> {%- endfor -%}
my code throws builderror: ('contactsapp.siteview', {}, none)
contactsapp = blueprint('contactsapp', __name__, template_folder='templates') @menu.register_menu(contactsapp, '.', 'list') class listview(methodview): def get(self): contacts = contact.objects.all() return render_template('contacts/list.html', contacts=contacts) @menu.register_menu(contactsapp, '.site', 'sites') class siteview(methodview): def get(self): sites = site.objects.all() return render_template('contacts/sites.html', sites=sites) contactsapp.add_url_rule('/', view_func=listview.as_view('list')) contactsapp.add_url_rule('/site/', view_func=siteview.as_view('sites'))
how can correctly decorate pluggable views? cheers.
there 2 options decorating view:
a. can decorate hand return value of as_view()
.
def user_required(f): """checks whether user logged in or raises error 401.""" def decorator(*args, **kwargs): if not g.user: abort(401) return f(*args, **kwargs) return decorator view = user_required(userapi.as_view('users')) app.add_url_rule('/users/', view_func=view)
b. if using flask 0.8+, can specify list of decorators apply in class declaration.
class userapi(methodview): decorators = [user_required]
Comments
Post a Comment