Skip to content

Commit

Permalink
Add to database
Browse files Browse the repository at this point in the history
  • Loading branch information
eleddy committed Jan 12, 2012
1 parent 03fbd3d commit 7d92ba6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
53 changes: 50 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,26 @@ called model.py and keep all of our access info there::
In this model, we will create the same todo item that we did in the web2py app with
a bit of a different twist. Edit model.py to say::

> XXX put stuff here
from flask import Flask
from flaskext.sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)


class TodoItem(db.Model):
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String(240), unique=True)

def __init__(self, description):
self.description = description

def __repr__(self):
return '<TODO %r>' % self.description

Next we need to initialize the database. Initializeing the database will sync the model
we created with the database, making sure that all the columns and tables we need are
there and ready to use*. In __init__.py::
Expand Down Expand Up @@ -253,8 +270,8 @@ __init__.py::
Keep in mind that at this moment the db is empty so a reload should just show an
empty list.

Adding Data to the Database
---------------------------
Submitting Data
---------------
Because this is our second time adding data to a database, let's also introduce the
concept of routing. Let's have our from page form submit to a url that is not the
index page, process the data, and then redirect. First things first, let's add a
Expand Down Expand Up @@ -283,6 +300,36 @@ in __init__.py modify the add function::

@app.route('/add', methods=['POST',])

Reload the front page and now you can see we are able to add an item and get redirected
to the new form!

Saving Data
-----------
Last but not the very least, we need to save the data. In __init__.py, get the data from
the REQUEST variable (we will discuss this in class) and then save to the database. The
commit is REQUIRED!::

@app.route('/add', methods=['POST',])
def add_todo():
if 'todo_item' in request.form:
todo = TodoItem(description=request.form['todo_item'])
db.session.add(todob)
db.session.commit()
return "Got it!"
return "Unknown Error"

Note that unlike web2py, there is no validation out of the box. This could be a good thing
or a bad things depending on your style and your project.

At this point you can go to the front page, add an item, then go back to to the front page
to see the repr version of this object. To show only the todo item, update hello.html::
<ul>
{% for todo in todos %}
<li>{{ todo.description }}</li>
{% endfor %}
<ul>

More Info
---------
Expand Down
7 changes: 6 additions & 1 deletion src/noiselist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ def index():

@app.route('/add', methods=['POST',])
def add_todo():
return "Made it!"
if 'todo_item' in request.form:
todo = TodoItem(description=request.form['todo_item'])
db.session.add(todob)
db.session.commit()
return "Got it!"
return "Unknown Error"

4 changes: 2 additions & 2 deletions src/noiselist/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class TodoItem(db.Model):
description = db.Column(db.String(240), unique=True)

def __init__(self, description):
self.description = todo_item
self.description = description


def __repr__(self):
return '<User %r>' % self.description
return '<TODO %r>' % self.description
2 changes: 1 addition & 1 deletion src/noiselist/templates/hello.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h1>Noiselist <small>Python web frameworks one list at a time</small></h1>
<h2>Current TODOs</h2>
<ul>
{% for todo in todos %}
<li>{{ todo }}</li>
<li>{{ todo.description }}</li>
{% endfor %}
<ul>
<form action="/add" method="POST" id="add_to_todo_list">
Expand Down

0 comments on commit 7d92ba6

Please sign in to comment.