How to call a function/method on Flask from the template
First of all, I'm quite new using flask, but this is something I haven't been able to find so far.
I'm working on my website with Flask and Jinja templates, using postgresql as a DB, I want to be able to call another function/method in my template.
Here I can get all my shares (posts)
@shares_app.route('/shares', methods=['GET'])
@login_required
def last_shares():
shares = fetch_last_shares()
form = ReusableForm(request.form)
return render_template('shares.html', form=form, shares=shares)
template
% for share in shares %
<li class="comment" style="border:1px solid black; padding:5px;" >
<a class="pull-left" href="#">
<img width="35" height="35" avatar="share[5]">
</a>
<div class="comment-body">
<div class="comment-heading">
<h4 class="user">share[5] (share[4]) </h4>
<h5 class="time">share[3] /</h5>
</div>
<p> <b>share[0] </b> / share[2]</p>
</div>
<!--comments here -->
Here I wanna be able to get all my comments related to shares, here its where I'm no sure if I can call another function from my controller.
comments = fetch_last_comments(share[0])
% for comment in comments %
Show comments here
% endfor %
<!--comments here -->
% endfor %
Basically, I want to be calling this function
def fetch_comments_by_shares(share_id):
comments = db.query("""SELECT * FROM comments WHERE share_id = """.format(share_id)).getresult()
return comments
Thanks a lot.
python python-3.x flask flask-wtforms
add a comment |
First of all, I'm quite new using flask, but this is something I haven't been able to find so far.
I'm working on my website with Flask and Jinja templates, using postgresql as a DB, I want to be able to call another function/method in my template.
Here I can get all my shares (posts)
@shares_app.route('/shares', methods=['GET'])
@login_required
def last_shares():
shares = fetch_last_shares()
form = ReusableForm(request.form)
return render_template('shares.html', form=form, shares=shares)
template
% for share in shares %
<li class="comment" style="border:1px solid black; padding:5px;" >
<a class="pull-left" href="#">
<img width="35" height="35" avatar="share[5]">
</a>
<div class="comment-body">
<div class="comment-heading">
<h4 class="user">share[5] (share[4]) </h4>
<h5 class="time">share[3] /</h5>
</div>
<p> <b>share[0] </b> / share[2]</p>
</div>
<!--comments here -->
Here I wanna be able to get all my comments related to shares, here its where I'm no sure if I can call another function from my controller.
comments = fetch_last_comments(share[0])
% for comment in comments %
Show comments here
% endfor %
<!--comments here -->
% endfor %
Basically, I want to be calling this function
def fetch_comments_by_shares(share_id):
comments = db.query("""SELECT * FROM comments WHERE share_id = """.format(share_id)).getresult()
return comments
Thanks a lot.
python python-3.x flask flask-wtforms
add a comment |
First of all, I'm quite new using flask, but this is something I haven't been able to find so far.
I'm working on my website with Flask and Jinja templates, using postgresql as a DB, I want to be able to call another function/method in my template.
Here I can get all my shares (posts)
@shares_app.route('/shares', methods=['GET'])
@login_required
def last_shares():
shares = fetch_last_shares()
form = ReusableForm(request.form)
return render_template('shares.html', form=form, shares=shares)
template
% for share in shares %
<li class="comment" style="border:1px solid black; padding:5px;" >
<a class="pull-left" href="#">
<img width="35" height="35" avatar="share[5]">
</a>
<div class="comment-body">
<div class="comment-heading">
<h4 class="user">share[5] (share[4]) </h4>
<h5 class="time">share[3] /</h5>
</div>
<p> <b>share[0] </b> / share[2]</p>
</div>
<!--comments here -->
Here I wanna be able to get all my comments related to shares, here its where I'm no sure if I can call another function from my controller.
comments = fetch_last_comments(share[0])
% for comment in comments %
Show comments here
% endfor %
<!--comments here -->
% endfor %
Basically, I want to be calling this function
def fetch_comments_by_shares(share_id):
comments = db.query("""SELECT * FROM comments WHERE share_id = """.format(share_id)).getresult()
return comments
Thanks a lot.
python python-3.x flask flask-wtforms
First of all, I'm quite new using flask, but this is something I haven't been able to find so far.
I'm working on my website with Flask and Jinja templates, using postgresql as a DB, I want to be able to call another function/method in my template.
Here I can get all my shares (posts)
@shares_app.route('/shares', methods=['GET'])
@login_required
def last_shares():
shares = fetch_last_shares()
form = ReusableForm(request.form)
return render_template('shares.html', form=form, shares=shares)
template
% for share in shares %
<li class="comment" style="border:1px solid black; padding:5px;" >
<a class="pull-left" href="#">
<img width="35" height="35" avatar="share[5]">
</a>
<div class="comment-body">
<div class="comment-heading">
<h4 class="user">share[5] (share[4]) </h4>
<h5 class="time">share[3] /</h5>
</div>
<p> <b>share[0] </b> / share[2]</p>
</div>
<!--comments here -->
Here I wanna be able to get all my comments related to shares, here its where I'm no sure if I can call another function from my controller.
comments = fetch_last_comments(share[0])
% for comment in comments %
Show comments here
% endfor %
<!--comments here -->
% endfor %
Basically, I want to be calling this function
def fetch_comments_by_shares(share_id):
comments = db.query("""SELECT * FROM comments WHERE share_id = """.format(share_id)).getresult()
return comments
Thanks a lot.
python python-3.x flask flask-wtforms
python python-3.x flask flask-wtforms
edited Nov 15 '18 at 21:33
Dinko Pehar
1,4513424
1,4513424
asked Nov 14 '18 at 6:12
ronron
299620
299620
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Instead of making multiple DB queries for each and every share id you can get all the comments for all the shares in the backend and then pass comments while rendering the template.
like.
render_template('shares.html', form=form, shares=shares, comments=comments)
and if still, you want to call the python function from jinja template then you can follow this answer for the same.
Call a python function from jinja2
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294133%2fhow-to-call-a-function-method-on-flask-from-the-template%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of making multiple DB queries for each and every share id you can get all the comments for all the shares in the backend and then pass comments while rendering the template.
like.
render_template('shares.html', form=form, shares=shares, comments=comments)
and if still, you want to call the python function from jinja template then you can follow this answer for the same.
Call a python function from jinja2
add a comment |
Instead of making multiple DB queries for each and every share id you can get all the comments for all the shares in the backend and then pass comments while rendering the template.
like.
render_template('shares.html', form=form, shares=shares, comments=comments)
and if still, you want to call the python function from jinja template then you can follow this answer for the same.
Call a python function from jinja2
add a comment |
Instead of making multiple DB queries for each and every share id you can get all the comments for all the shares in the backend and then pass comments while rendering the template.
like.
render_template('shares.html', form=form, shares=shares, comments=comments)
and if still, you want to call the python function from jinja template then you can follow this answer for the same.
Call a python function from jinja2
Instead of making multiple DB queries for each and every share id you can get all the comments for all the shares in the backend and then pass comments while rendering the template.
like.
render_template('shares.html', form=form, shares=shares, comments=comments)
and if still, you want to call the python function from jinja template then you can follow this answer for the same.
Call a python function from jinja2
edited Nov 14 '18 at 6:35
answered Nov 14 '18 at 6:29
RamRam
385112
385112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294133%2fhow-to-call-a-function-method-on-flask-from-the-template%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown