Created
May 12, 2016 18:38
-
-
Save liamzdenek/5fcfc618e7ace21a56a4f5ba56c12426 to your computer and use it in GitHub Desktop.
Flask template with Left Join
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% set cur_msg_id = -1 %} | |
{% for row in rows %} | |
{% if cur_msg_id != row['message_id'] %} | |
{% set cur_msg_id = row['message_id'] %} | |
Message: {{ row['message_id'] }}<br/> | |
{% endif %} | |
{% if row['comment_id'] != None %} | |
{{ row['first_name'] }} {{ row['last_name'] }}: {{ row['comment'] }}<br/> | |
{% endif %} | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template | |
# import the Connector function | |
from mysqlconnection import MySQLConnector | |
app = Flask(__name__) | |
# connect and store the connection in "mysql" note that you pass the database name to the function | |
mysql = MySQLConnector(app, 'mydb') | |
# an example of running a query | |
# print mysql.query_db("SELECT * FROM messages") | |
app.jinja_env.add_extension('jinja2.ext.loopcontrols') | |
@app.route('/') | |
def index(): | |
query = 'SELECT messages.id as message_id, message, comments.id as comment_id, comments.comment as comment, users.id as user_id, first_name, last_name, email FROM messages LEFT JOIN comments ON(messages.id = comments.message_id) LEFT JOIN users ON(comments.user_id = users.id)'; | |
rows = mysql.query_db(query); | |
return render_template('index.html', rows=rows); | |
app.run(debug=True, host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment