Skip to content

Instantly share code, notes, and snippets.

@akaak akaak/
Created Feb 11, 2016

What would you like to do?
Flask application with a Form, Model, Database (SQLAlchemy for ORM). Uses modules structure rather than a package structure.
from flask import Flask, url_for
from flask import request, render_template, flash, redirect
from forms import BizForm, AddBiz
from flask.ext.sqlalchemy import SQLAlchemy
from import Security, utils
from contextlib import closing
from datetime import date
import forms
# Create app and load the config
app = Flask(__name__)
# connect to the database
db = SQLAlchemy(app)
# the "models" import SHOULD be after the above "db" assignment
import models
# 'from models import *' works but 'from models import Business' does NOT work
# dont know why!
from models import *
# run db.create_all before running the app to create DB Tables
def index():
return render_template('index.html',
def new_biz():
form = AddBiz(csrf_enabled=True)
#form_biz = Bizs()
if form.validate_on_submit():
new_biz = Business(,,
return redirect(url_for('biz_list'))
return render_template('biz.html',form=form)
def biz_list():
biz_list = Business.query.all()
#biz_list = {'name': '1.1', 'desc': '2.2'}
return render_template('biz_list.html', bizs=biz_list)
if __name__ == '__main__':
<!-- extend base layout -->
{% extends "base.html" %}
{% block content %}
<h2>Add Business</h2>
<div class='edit well offset 2 span8'>
<form method='post' class="form-horizontal">
{{ form.hidden_tag() }}
{{ edit_field( , class="span3") }}
{{ edit_field(form.description, rows="5" ,class="span3"
, placeholder="foobar" ) }}
{{ edit_field(form.added_date, class="span3",type="datetime") }}
<div class="form-actions">
<button type="submit" class="btn">SAVE</button>
{% endblock %}
<!-- extend base layout -->
{% extends "base.html" %}
{% block content %}
<h2>Business List</h2>
{% for biz in bizs %}
<tr><td><b>{{ }}</b></td>
<td>{{ biz.description }}</td></tr>
{% endfor %}
{% endblock %}
DEBUG = True
SECRET_KEY = '6e9d6e59ad52278806294952fbd3a263'
SQLALCHEMY_DATABASE_URI = 'sqlite:///bizdb.db'
from import Form
from wtforms import StringField, DateField, IntegerField, \
TextAreaField, TextField, validators
from wtforms.validators import required, DataRequired, InputRequired
class AddBiz(Form):
name = TextField('Biz Name',[validators.Length(min=4, max=80)])
description = TextAreaField('Description',validators=[required()])
added_date = DateField('When did you add this business? (mm/dd/yyyy)',
class BizForm(Form):
bizid = IntegerField('bizid', validators=[DataRequired()])
bizname = StringField('bizname')
from flask_sqlalchemy import SQLAlchemy
from app import db
import datetime
# cannot have the following db assignment in the
#db = SQLAlchemy()
class Business(db.Model):
#__tablename__ = "bizs"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
description = db.Column(db.Text, nullable=False)
added_date = db.Column(db.DATE, nullable=False)
def __init__(self,name,description, added_date): = name
self.description = description
self.added_date = added_date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.