Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add to database

  • Loading branch information...
commit 7d92ba6366e44d5197da115353e5eeae0222c755 1 parent 03fbd3d
@eleddy eleddy authored
View
53 README.rst
@@ -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::
@@ -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
@@ -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
---------
View
7 src/noiselist/__init__.py
@@ -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"
View
4 src/noiselist/model.py
@@ -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
View
2  src/noiselist/templates/hello.html
@@ -34,7 +34,7 @@
<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">
Please sign in to comment.
Something went wrong with that request. Please try again.