flask-navigation icon indicating copy to clipboard operation
flask-navigation copied to clipboard

args callable is accessed ouside application/request context

Open noirbizarre opened this issue 10 years ago • 2 comments

Actually it's not possible to write:

from flask import g

args_getter = lambda: {'user': g.current_user}

nav.Bar('usermenu', (
    nav.Item(_('Dashboard'), 'user.dashboard', args_getter),
    nav.Item(_('Profile'), 'user.profile', args_getter),
    nav.Item(_('Settings'), 'user.settings', args_getter),
))

Args are evaluated on navbar creation to fill the ident but they should only be accessed on rendering to ensure app/request context is pushed.

I will try to submit a pull-request to fix this behavior but idents need to be changed.

Another solution is to provide a callable to generate url like that:

<a href="{{ item.url_for(user=current_user) }}">{{ item.label }}</a>

noirbizarre avatar Jun 03 '14 10:06 noirbizarre

@noirbizarre Thank you for your feedback. It's my wrong indeep.

Actually I think the ident composed with endpoint and args is a bad idea after the v0.1.0 released. I regreted for that and wish to give all items explicit names instead of auto-generated ident tuples in next major version.

Would you mind to use

<a href="{{ url_for(item.endpoint, user=current_user) }}">{{ item.label }}</a>

as a temporary solution? I am working for the next version and I will release it as soon as possible.

tonyseek avatar Jun 03 '14 11:06 tonyseek

In fact I've written it like that:

from flask import g, Blueprint

bp = Blueprint('test', __name__)

navbar = nav.Bar('usermenu', (
    nav.Item(_('Dashboard'), 'user.dashboard'),
    nav.Item(_('Profile'), 'user.profile'),
    nav.Item(_('Settings'), 'user.settings'),
))

@bp.route('/')
def my_view():
    for item in navbar:
        item._args = {'user': g.current_user}
    return render_template('index.html')

But you're right, I can use the registered endpoint too.

noirbizarre avatar Jun 03 '14 13:06 noirbizarre