Skip to content

Instantly share code, notes, and snippets.

@faruken
Created July 16, 2012 03:44
Show Gist options
  • Save faruken/3120343 to your computer and use it in GitHub Desktop.
Save faruken/3120343 to your computer and use it in GitHub Desktop.
Flask meets web.py
<form action="{{url_for('add')}}" method="post">
<p><label for="title">title:</label><input type="text" name="title" id="title"/></p>
<p><label for="text">text</label><input type="text" name="text" id="text"/>
</p>
<p><input type="submit" value="send"/></p>
</form>
<p>{{entry.title}}</p>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from web import database
from flask import Flask, render_template, abort, flash, redirect, url_for, request
DATABASE = '/tmp/flaskr.db'
SECRET_KEY = 'what'
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
conn = database(dbn='sqlite', db=DATABASE)
@app.route('/')
def index():
entries = conn.select('entries',
what='id, title, text',
limit=20,
order='id DESC')
return render_template('index.html', entries=entries)
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'POST':
conn.insert('entries',
title=request.form['title'],
text=request.form['text'])
flash('a new entry')
return redirect(url_for('index'))
return render_template('add.html')
@app.route('/entry/<int:entry_id>')
def show_entry(entry_id):
entry = conn.select('entries',
what='id, title, text',
where='id = $id',
vars={'id': entry_id},
limit=1)
try:
return render_template('entry.html', entry=entry[0])
except IndexError:
return abort(404)
def a():
q = """
create table entries(
id integer primary key autoincrement,
title string not null,
text string not null
);
"""
if __name__ == '__main__':
app.run(debug=DEBUG)
<p><a href="/add">+ add</a></p>
{% for msg in get_flashed_messages() %}
<div>{{msg}}</div>
{% endfor %}<br />
{% for entry in entries %}
<p>{{entry.id}} {{entry.title}} {{entry.text}}</p>
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment