Skip to content

Instantly share code, notes, and snippets.

@akaak akaak/app.py
Created Feb 11, 2016

Embed
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 flask.ext.security import Security, utils
from contextlib import closing
from datetime import date
import forms
# Create app and load the config
app = Flask(__name__)
app.config.from_object('config')
# 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
@app.route('/')
@app.route('/index')
def index():
return render_template('index.html',
title='Home')
@app.route('/add/',methods=['GET','POST'])
def new_biz():
form = AddBiz(csrf_enabled=True)
#form_biz = Bizs()
if form.validate_on_submit():
new_biz = Business(
form.name.data,
form.description.data,
form.added_date.data
)
db.session.add(new_biz)
db.session.commit()
return redirect(url_for('biz_list'))
return render_template('biz.html',form=form)
@app.route('/bizlist',methods=['GET','POST'])
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__':
app.run(
host='127.0.0.1',
port=int('8080'),
debug=app.config['DEBUG']
)
<!-- 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(form.name , 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>
</div>
</form>
</div>
{% endblock %}
<!-- extend base layout -->
{% extends "base.html" %}
{% block content %}
<h2>Business List</h2>
<table>
{% for biz in bizs %}
<p>
<tr><td><b>{{ biz.name }}</b></td>
<td>{{ biz.description }}</td></tr>
</p>
{% endfor %}
</table>
{% endblock %}
CSRF_ENABLED = True
DEBUG = True
SECRET_KEY = '6e9d6e59ad52278806294952fbd3a263'
SQLALCHEMY_DATABASE_URI = 'sqlite:///bizdb.db'
from flask.ext.wtf 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)',
validators=[InputRequired()],format='%m/%d/%Y')
class BizForm(Form):
bizid = IntegerField('bizid', validators=[DataRequired()])
bizname = StringField('bizname')
# models.py
from flask_sqlalchemy import SQLAlchemy
from app import db
import datetime
# cannot have the following db assignment in the models.py
#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):
self.name = 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.