Issue
So I have this Flask app that I need a pass a particular user to all routes as all pages in the app will be needing the particular user to render templates. I don't want to pass the user as it's normal done
return render_template('index.html',user=user)
Because I'd have to repeat the same thing for all routes in the render_templates. Please how can I do this?
Solution
You can do so by creating a custom render template function using the following implementation
from flask import render_template as real_render_template
from yourapp import user # Import the user variable or set it
def render_template(*args, **kwargs):
return real_render_template(*args, **kwargs, user=user)
it can also be done via functools.partial
:
from flask import render_template as real_render_template
from yourapp import user # Import the user variable or set it
from functools import partial
render_template = partial(real_render_template, user=user)
Answered By - xcodz-dot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.