Render_template second time after page is already loaded









up vote
1
down vote

favorite












I am trying to query a database through my website and dynamically add columns with the results using Jinja templates. I am using flask and on my views function. I am rendering the values like this



return render_template('query.html',my_list=my_list )



The thing is that when I load the page the user hasn't queried the database yet, so my_list is empty. The user queries the database by pressing a button on the html page and making an ajax request through jquery. My question now is if its possible to return my_list using jinja templates even after the page is already loaded, meaning I have to return render_template a second time (after submit button) to get the values for my_list.



p.s. I prefer not to use json parsing.



Here is some sample code. I query the database with the values of intranetID of column SubmitterID and platform of column Platforms to return the whole entry to the database with the values of the rest of the columns as well.



@app.route('/querydbvalues',methods=['POST', 'GET'])
def querydbvalues():
if request.method == 'POST' or request.method == 'GET':
results = models.mydatabase.query.filter_by(SubmitterID=qIntranetID,Platforms=qPlatform).all()
my_list = [i.user for i in results]
return render_template('query.html',my_list=my_list )


and on my html page



% for n in my_list %
<li>n</li>
% endfor %


on the Javascript file when you press the button i make the ajax request like this



$("#SearchDatabase").click(function()
var tmp = document.getElementById("qIntranetID").value;
var tmp2 = document.getElementById("qPlatform").value;
jQuery.ajax(
dataType: "json",
url:"/querydbvalues", //tell the script where to send requests
data:text:tmp,text2:tmp2,
type:'GET',
contentType: 'application/json',
success: function(results)
//do something

);
);









share|improve this question























  • You don't need to check the request method. Flask will only route GET and POST requests to that endpoint.
    – dirn
    Mar 24 '16 at 22:32















up vote
1
down vote

favorite












I am trying to query a database through my website and dynamically add columns with the results using Jinja templates. I am using flask and on my views function. I am rendering the values like this



return render_template('query.html',my_list=my_list )



The thing is that when I load the page the user hasn't queried the database yet, so my_list is empty. The user queries the database by pressing a button on the html page and making an ajax request through jquery. My question now is if its possible to return my_list using jinja templates even after the page is already loaded, meaning I have to return render_template a second time (after submit button) to get the values for my_list.



p.s. I prefer not to use json parsing.



Here is some sample code. I query the database with the values of intranetID of column SubmitterID and platform of column Platforms to return the whole entry to the database with the values of the rest of the columns as well.



@app.route('/querydbvalues',methods=['POST', 'GET'])
def querydbvalues():
if request.method == 'POST' or request.method == 'GET':
results = models.mydatabase.query.filter_by(SubmitterID=qIntranetID,Platforms=qPlatform).all()
my_list = [i.user for i in results]
return render_template('query.html',my_list=my_list )


and on my html page



% for n in my_list %
<li>n</li>
% endfor %


on the Javascript file when you press the button i make the ajax request like this



$("#SearchDatabase").click(function()
var tmp = document.getElementById("qIntranetID").value;
var tmp2 = document.getElementById("qPlatform").value;
jQuery.ajax(
dataType: "json",
url:"/querydbvalues", //tell the script where to send requests
data:text:tmp,text2:tmp2,
type:'GET',
contentType: 'application/json',
success: function(results)
//do something

);
);









share|improve this question























  • You don't need to check the request method. Flask will only route GET and POST requests to that endpoint.
    – dirn
    Mar 24 '16 at 22:32













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to query a database through my website and dynamically add columns with the results using Jinja templates. I am using flask and on my views function. I am rendering the values like this



return render_template('query.html',my_list=my_list )



The thing is that when I load the page the user hasn't queried the database yet, so my_list is empty. The user queries the database by pressing a button on the html page and making an ajax request through jquery. My question now is if its possible to return my_list using jinja templates even after the page is already loaded, meaning I have to return render_template a second time (after submit button) to get the values for my_list.



p.s. I prefer not to use json parsing.



Here is some sample code. I query the database with the values of intranetID of column SubmitterID and platform of column Platforms to return the whole entry to the database with the values of the rest of the columns as well.



@app.route('/querydbvalues',methods=['POST', 'GET'])
def querydbvalues():
if request.method == 'POST' or request.method == 'GET':
results = models.mydatabase.query.filter_by(SubmitterID=qIntranetID,Platforms=qPlatform).all()
my_list = [i.user for i in results]
return render_template('query.html',my_list=my_list )


and on my html page



% for n in my_list %
<li>n</li>
% endfor %


on the Javascript file when you press the button i make the ajax request like this



$("#SearchDatabase").click(function()
var tmp = document.getElementById("qIntranetID").value;
var tmp2 = document.getElementById("qPlatform").value;
jQuery.ajax(
dataType: "json",
url:"/querydbvalues", //tell the script where to send requests
data:text:tmp,text2:tmp2,
type:'GET',
contentType: 'application/json',
success: function(results)
//do something

);
);









share|improve this question















I am trying to query a database through my website and dynamically add columns with the results using Jinja templates. I am using flask and on my views function. I am rendering the values like this



return render_template('query.html',my_list=my_list )



The thing is that when I load the page the user hasn't queried the database yet, so my_list is empty. The user queries the database by pressing a button on the html page and making an ajax request through jquery. My question now is if its possible to return my_list using jinja templates even after the page is already loaded, meaning I have to return render_template a second time (after submit button) to get the values for my_list.



p.s. I prefer not to use json parsing.



Here is some sample code. I query the database with the values of intranetID of column SubmitterID and platform of column Platforms to return the whole entry to the database with the values of the rest of the columns as well.



@app.route('/querydbvalues',methods=['POST', 'GET'])
def querydbvalues():
if request.method == 'POST' or request.method == 'GET':
results = models.mydatabase.query.filter_by(SubmitterID=qIntranetID,Platforms=qPlatform).all()
my_list = [i.user for i in results]
return render_template('query.html',my_list=my_list )


and on my html page



% for n in my_list %
<li>n</li>
% endfor %


on the Javascript file when you press the button i make the ajax request like this



$("#SearchDatabase").click(function()
var tmp = document.getElementById("qIntranetID").value;
var tmp2 = document.getElementById("qPlatform").value;
jQuery.ajax(
dataType: "json",
url:"/querydbvalues", //tell the script where to send requests
data:text:tmp,text2:tmp2,
type:'GET',
contentType: 'application/json',
success: function(results)
//do something

);
);






python ajax flask rendering jinja2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 10:13









Flimzy

36.7k96496




36.7k96496










asked Mar 24 '16 at 18:55









PetrosM

17810




17810











  • You don't need to check the request method. Flask will only route GET and POST requests to that endpoint.
    – dirn
    Mar 24 '16 at 22:32

















  • You don't need to check the request method. Flask will only route GET and POST requests to that endpoint.
    – dirn
    Mar 24 '16 at 22:32
















You don't need to check the request method. Flask will only route GET and POST requests to that endpoint.
– dirn
Mar 24 '16 at 22:32





You don't need to check the request method. Flask will only route GET and POST requests to that endpoint.
– dirn
Mar 24 '16 at 22:32













1 Answer
1






active

oldest

votes

















up vote
0
down vote













The Ajax call is a completely separate request. You can do what you want in that request, including render templates.






share|improve this answer




















  • Hi, thanks for your input. However my problem is that i want to render "my_list" on the same html page without refreshing or changing to another page. And for some reason i cant and thats why i asked for help
    – PetrosM
    Mar 24 '16 at 19:06










  • Well that is what Ajax does. You should maybe explain why you're having problems rendering the same page, rather than saying "for some reason I can't".
    – Daniel Roseman
    Mar 24 '16 at 19:29











  • i dont have problem rendering the same page. I have problem rendering the value on "my_list" (the second argument i render after the html page) and using those values with jinja template on my webpage.
    – PetrosM
    Mar 24 '16 at 19:31










  • You're going to need to show some code.
    – Daniel Roseman
    Mar 24 '16 at 19:31






  • 1




    Hello, first of all thank you for baring with me and still trying to help me. by "return render_template('query.html',my_list=my_list )" i return my results my_list and with jinja templating i can just use the my_list values on my html page like i described on the code above. however when the page is first loaded my_list = and only changes values when the user presses submit and the querying to the database happens. Once i get the values i want to my_list i want to return them again to use with jinja templating. This doesn't happen and i cant return the new values of my_list.
    – PetrosM
    Mar 25 '16 at 13:40











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',
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36207962%2frender-template-second-time-after-page-is-already-loaded%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








up vote
0
down vote













The Ajax call is a completely separate request. You can do what you want in that request, including render templates.






share|improve this answer




















  • Hi, thanks for your input. However my problem is that i want to render "my_list" on the same html page without refreshing or changing to another page. And for some reason i cant and thats why i asked for help
    – PetrosM
    Mar 24 '16 at 19:06










  • Well that is what Ajax does. You should maybe explain why you're having problems rendering the same page, rather than saying "for some reason I can't".
    – Daniel Roseman
    Mar 24 '16 at 19:29











  • i dont have problem rendering the same page. I have problem rendering the value on "my_list" (the second argument i render after the html page) and using those values with jinja template on my webpage.
    – PetrosM
    Mar 24 '16 at 19:31










  • You're going to need to show some code.
    – Daniel Roseman
    Mar 24 '16 at 19:31






  • 1




    Hello, first of all thank you for baring with me and still trying to help me. by "return render_template('query.html',my_list=my_list )" i return my results my_list and with jinja templating i can just use the my_list values on my html page like i described on the code above. however when the page is first loaded my_list = and only changes values when the user presses submit and the querying to the database happens. Once i get the values i want to my_list i want to return them again to use with jinja templating. This doesn't happen and i cant return the new values of my_list.
    – PetrosM
    Mar 25 '16 at 13:40















up vote
0
down vote













The Ajax call is a completely separate request. You can do what you want in that request, including render templates.






share|improve this answer




















  • Hi, thanks for your input. However my problem is that i want to render "my_list" on the same html page without refreshing or changing to another page. And for some reason i cant and thats why i asked for help
    – PetrosM
    Mar 24 '16 at 19:06










  • Well that is what Ajax does. You should maybe explain why you're having problems rendering the same page, rather than saying "for some reason I can't".
    – Daniel Roseman
    Mar 24 '16 at 19:29











  • i dont have problem rendering the same page. I have problem rendering the value on "my_list" (the second argument i render after the html page) and using those values with jinja template on my webpage.
    – PetrosM
    Mar 24 '16 at 19:31










  • You're going to need to show some code.
    – Daniel Roseman
    Mar 24 '16 at 19:31






  • 1




    Hello, first of all thank you for baring with me and still trying to help me. by "return render_template('query.html',my_list=my_list )" i return my results my_list and with jinja templating i can just use the my_list values on my html page like i described on the code above. however when the page is first loaded my_list = and only changes values when the user presses submit and the querying to the database happens. Once i get the values i want to my_list i want to return them again to use with jinja templating. This doesn't happen and i cant return the new values of my_list.
    – PetrosM
    Mar 25 '16 at 13:40













up vote
0
down vote










up vote
0
down vote









The Ajax call is a completely separate request. You can do what you want in that request, including render templates.






share|improve this answer












The Ajax call is a completely separate request. You can do what you want in that request, including render templates.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 '16 at 18:58









Daniel Roseman

441k40573627




441k40573627











  • Hi, thanks for your input. However my problem is that i want to render "my_list" on the same html page without refreshing or changing to another page. And for some reason i cant and thats why i asked for help
    – PetrosM
    Mar 24 '16 at 19:06










  • Well that is what Ajax does. You should maybe explain why you're having problems rendering the same page, rather than saying "for some reason I can't".
    – Daniel Roseman
    Mar 24 '16 at 19:29











  • i dont have problem rendering the same page. I have problem rendering the value on "my_list" (the second argument i render after the html page) and using those values with jinja template on my webpage.
    – PetrosM
    Mar 24 '16 at 19:31










  • You're going to need to show some code.
    – Daniel Roseman
    Mar 24 '16 at 19:31






  • 1




    Hello, first of all thank you for baring with me and still trying to help me. by "return render_template('query.html',my_list=my_list )" i return my results my_list and with jinja templating i can just use the my_list values on my html page like i described on the code above. however when the page is first loaded my_list = and only changes values when the user presses submit and the querying to the database happens. Once i get the values i want to my_list i want to return them again to use with jinja templating. This doesn't happen and i cant return the new values of my_list.
    – PetrosM
    Mar 25 '16 at 13:40

















  • Hi, thanks for your input. However my problem is that i want to render "my_list" on the same html page without refreshing or changing to another page. And for some reason i cant and thats why i asked for help
    – PetrosM
    Mar 24 '16 at 19:06










  • Well that is what Ajax does. You should maybe explain why you're having problems rendering the same page, rather than saying "for some reason I can't".
    – Daniel Roseman
    Mar 24 '16 at 19:29











  • i dont have problem rendering the same page. I have problem rendering the value on "my_list" (the second argument i render after the html page) and using those values with jinja template on my webpage.
    – PetrosM
    Mar 24 '16 at 19:31










  • You're going to need to show some code.
    – Daniel Roseman
    Mar 24 '16 at 19:31






  • 1




    Hello, first of all thank you for baring with me and still trying to help me. by "return render_template('query.html',my_list=my_list )" i return my results my_list and with jinja templating i can just use the my_list values on my html page like i described on the code above. however when the page is first loaded my_list = and only changes values when the user presses submit and the querying to the database happens. Once i get the values i want to my_list i want to return them again to use with jinja templating. This doesn't happen and i cant return the new values of my_list.
    – PetrosM
    Mar 25 '16 at 13:40
















Hi, thanks for your input. However my problem is that i want to render "my_list" on the same html page without refreshing or changing to another page. And for some reason i cant and thats why i asked for help
– PetrosM
Mar 24 '16 at 19:06




Hi, thanks for your input. However my problem is that i want to render "my_list" on the same html page without refreshing or changing to another page. And for some reason i cant and thats why i asked for help
– PetrosM
Mar 24 '16 at 19:06












Well that is what Ajax does. You should maybe explain why you're having problems rendering the same page, rather than saying "for some reason I can't".
– Daniel Roseman
Mar 24 '16 at 19:29





Well that is what Ajax does. You should maybe explain why you're having problems rendering the same page, rather than saying "for some reason I can't".
– Daniel Roseman
Mar 24 '16 at 19:29













i dont have problem rendering the same page. I have problem rendering the value on "my_list" (the second argument i render after the html page) and using those values with jinja template on my webpage.
– PetrosM
Mar 24 '16 at 19:31




i dont have problem rendering the same page. I have problem rendering the value on "my_list" (the second argument i render after the html page) and using those values with jinja template on my webpage.
– PetrosM
Mar 24 '16 at 19:31












You're going to need to show some code.
– Daniel Roseman
Mar 24 '16 at 19:31




You're going to need to show some code.
– Daniel Roseman
Mar 24 '16 at 19:31




1




1




Hello, first of all thank you for baring with me and still trying to help me. by "return render_template('query.html',my_list=my_list )" i return my results my_list and with jinja templating i can just use the my_list values on my html page like i described on the code above. however when the page is first loaded my_list = and only changes values when the user presses submit and the querying to the database happens. Once i get the values i want to my_list i want to return them again to use with jinja templating. This doesn't happen and i cant return the new values of my_list.
– PetrosM
Mar 25 '16 at 13:40





Hello, first of all thank you for baring with me and still trying to help me. by "return render_template('query.html',my_list=my_list )" i return my results my_list and with jinja templating i can just use the my_list values on my html page like i described on the code above. however when the page is first loaded my_list = and only changes values when the user presses submit and the querying to the database happens. Once i get the values i want to my_list i want to return them again to use with jinja templating. This doesn't happen and i cant return the new values of my_list.
– PetrosM
Mar 25 '16 at 13:40


















draft saved

draft discarded
















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36207962%2frender-template-second-time-after-page-is-already-loaded%23new-answer', 'question_page');

);

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







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20