简体   繁体   中英

Handling g.user variable undefined errors?

Within our jinja templates we have a block of code that tries to create a span for the current user:

<span class="fa fa-user"></span> {{g.user.get_full_name()}}

However, if the user happens to clear the cookies on his browser, this g.user.get_full_name function will no longer exist, which causes the app to crash.

I don't necessarily want to reconstruct/preserve the g.user local proxy on cookie clear, but I at least want the app to handle it gracefully, maybe with a redirection to the login page. Is there any way to catch these exceptions for an arbitrary g.user.x reference?

You can use something like

{{g.user.get_full_name() if g.user is not None else "do something"}}

or add the check on the back end and redirect when it is not present

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM