Created
July 16, 2018 20:13
-
-
Save tachyondecay/adecab97f2f123ac3c4ca8cb16c2b46f to your computer and use it in GitHub Desktop.
quick example
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 app import app, db # DB is your Flask-SQLAlchemy object | |
# By default this will correspond to a table named "ticket" in your db | |
# You can override this, of course (see the docs) to point to any tablename | |
class Ticket(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
date = db.Column(db.DateTime, nullable=False) | |
severity = db.Column(db.Integer, nullable=False, default=0) | |
message = db.Column(db.String) | |
# No constructor needed, generally, unless you're doing something extra! | |
# SQLAlchemy handles all that stuff for you | |
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
<table> | |
<thead> | |
<tr> | |
<th>ID</th> | |
<th>Date</th> | |
<th>Message</th> | |
</tr> | |
</thead> | |
<tbody> | |
{% for t in tickets %} | |
<tr><td>{{ t.id }}</td><td>{{ t.date }}</td><td>{{ t.message }}</td></tr> | |
{% endfor %} | |
</tbody> | |
</table> |
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 app import app, db | |
from models import Ticket | |
from datetime import datetime | |
@app.route('/add/', methods=['POST', 'GET']): | |
def add(): | |
# Check for form submission, validate, etc.--I recommend WTForms | |
# Let's assume you've got valid, sanitized input and want to enter a new ticket: | |
t = Ticket(date=datetime.utcnow(), severity=request.form['severity'], message=request.form['message']) | |
db.session.add(t) | |
db.session.commit() | |
# Send user a success page | |
@app.route('/view/'): | |
def view(): | |
tickets = Ticket.query.order_by(Ticket.date.desc()).all() # Get all tickets ordered by newest first | |
return render_template('view.html', tickets=tickets) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment