[Flask] Passing MySQL values to different webpages

Mazzei, Stephen Andrew Stephen.Mazzei at asrcfederal.com
Fri Mar 11 14:23:36 EST 2016


Maybe you can answer this, (being completely new to flask/html/etc) the session helped pass variables through the flask program inside the python scripts. How can I pass variables from the html template? Would that be more the method you originally sent?

I am trying to build a basic template that can be reused depending on what http link was clicked from the previous page.

Example:

Page 1
“link to apple”
“link to orange”

Page 2
                You chose (VARIABLE)

Looking online it looks like I could do

Page 1
<a href='template.html?myVar1=apple'>Apple</a>
<a href='template.html?myVar1=apple'>Orange</a>

Page 2
                You chose (myVar1) ==> You chose Apple

Not sure on the python/flask side how to capture the ?myVar1?


---
Stephen A. Mazzei
Systems Administrator | AFDS, ASRC Federal Data Solutions - P&G HPC Account | 513-634-9965

From: Brian Kim [mailto:bk at breadtech.com]
Sent: Friday, March 11, 2016 2:16 PM
To: Mazzei, Stephen Andrew <Stephen.Mazzei at asrcfederal.com>
Cc: flask at python.org
Subject: Re: [Flask] Passing MySQL values to different webpages

Nice! Using a session variable is definitely a better solution than what I was suggesting.

On Mar 11, 2016, at 1:52 PM, Mazzei, Stephen Andrew <Stephen.Mazzei at asrcfederal.com<mailto:Stephen.Mazzei at asrcfederal.com>> wrote:
Thanks Brian, but I was able to do this using Flask/session.

@app.route(‘/page1’)
def page1():
    session[‘var1’] = “Some value”
    return redirect(url_for(app.page2))

@app.route(‘/page2’)
def page2():
    return render_template(“template.html”, value2=session.get('val1', None))


---
Stephen A. Mazzei
Systems Administrator | AFDS, ASRC Federal Data Solutions - P&G HPC Account | 513-634-9965

From: Brian Kim [mailto:bk at breadtech.com]
Sent: Friday, March 11, 2016 11:36 AM
To: Mazzei, Stephen Andrew <Stephen.Mazzei at asrcfederal.com<mailto:Stephen.Mazzei at asrcfederal.com>>
Cc: flask at python.org<mailto:flask at python.org>
Subject: Re: [Flask] Passing MySQL values to different webpages

Hi Stephen,

I believe that the best way to solve your problem will be to pass the data from page1 into page2 through the URI via GET request. In order to dynamically set the values, you will probably need to use Javascript. For example (with JQuery for syntactic convenience...)

mypage1.html:
<button id="go">Go to Page 2</button>
<script>
  $("#go").click( function() {
     target = "/mypage2?" + "key1=" + mydata[0] + "&key2=" + mydata[1]; // build the URI
     location.href = target; // redirect the page to that URL
   }
</script>

myapp.py:
@app.route("/mypage2")
def mypage2():
  data1 = request.args.get("key1")
  data2 = request.args.get("key2")
  return render_template("mypage2.html", key1=data1, key2=data2)

This isn't a complete by any means but hopefully gives enough to get you started... Let me know if you would like a better example and I'll try to put something up on github for you.

BK

On Fri, Mar 11, 2016 at 10:30 AM, Mazzei, Stephen Andrew <Stephen.Mazzei at asrcfederal.com<mailto:Stephen.Mazzei at asrcfederal.com>> wrote:
Good morning,

I am looking for help on a MySQL issue. I am currently running an query over a database and populating a table. The table contains items group units and then their values. I currently have the group unit as a link to that units own webpage.

On this webpage I would like to repost the same table information, per the unit, and then some additional information. How can I pass the query from the previous webpage to the next page without having to re-run the same query over and over again.

Thank you


---
Stephen A. Mazzei
Systems Administrator | AFDS, ASRC Federal Data Solutions - P&G HPC Account | 513-634-9965<tel:513-634-9965>


________________________________

The preceding message (including attachments) is covered by the Electronic Communication Privacy Act, 18 U.S.C. sections 2510-2512, is intended only for the person or entity to which it is addressed, and may contain information that is confidential, protected by attorney-client or other privilege, or otherwise protected from disclosure by law. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited. Please reply to the sender that you have received the message in error and destroy the original message and all copies.

_______________________________________________
Flask mailing list
Flask at python.org<mailto:Flask at python.org>
https://mail.python.org/mailman/listinfo/flask


________________________________

The preceding message (including attachments) is covered by the Electronic Communication Privacy Act, 18 U.S.C. sections 2510-2512, is intended only for the person or entity to which it is addressed, and may contain information that is confidential, protected by attorney-client or other privilege, or otherwise protected from disclosure by law. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited. Please reply to the sender that you have received the message in error and destroy the original message and all copies.

________________________________

The preceding message (including attachments) is covered by the Electronic Communication Privacy Act, 18 U.S.C. sections 2510-2512, is intended only for the person or entity to which it is addressed, and may contain information that is confidential, protected by attorney-client or other privilege, or otherwise protected from disclosure by law. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited. Please reply to the sender that you have received the message in error and destroy the original message and all copies.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160311/fc6d89a9/attachment-0001.html>


More information about the Flask mailing list