To use our talks database in our application we need to define a model. A model basically tells our Python application what the corresponding table in our database looks like, including what the field names are and what type of data is stored in them. If our database had multiple tables, we would create multiple models.
We define our model with peewee like this:
class Talk(Model):
title = CharField()
description = CharField()
created_at = DateField()
class Meta:
database = db
See the full code for app.py up to this point:
# app.py
from flask import Flask, render_template, redirect, url_for, jsonify, request
from talk_mashup_bot import talk_mashup_bot
from peewee import *
app = Flask(__name__)
db = SqliteDatabase('talks.db')
@app.route('/')
def hello():
return "Hello world!"
@app.route('/talk/new')
def new_talk():
title = talk_mashup_bot.generateTitle()
return render_template('new_talk.html', title=title)
@app.route('/talk', methods=['POST', 'GET'])
def talk():
if request.method == 'GET':
return redirect(url_for('new_talk'))
talk_title = request.form['form-title']
talk_description = request.form['form-description']
return render_template('talk.html', title=talk_title, description=talk_description)
class Talk(Model):
title = CharField()
description = CharField()
created_at = DateField()
class Meta:
database = db
if __name__ == '__main__':
app.run(debug=True)