Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Hook up to a template

  • Loading branch information...
commit 274814e789a51184e9bec9c79377be8ce49edbb5 1 parent ba4ebf1
Elizabeth Leddy eleddy authored
77 README.rst
Source Rendered
@@ -4,13 +4,19 @@ Introduction
4 4 This is the second installment of our tour of web frameworks. This will
5 5 be a simple TODO list.
6 6
  7 +
  8 +Up and Running
  9 +--------------
7 10 Just like in the last class, you want to run bootstrap and buildout::
8   - > git clone <location to repo here>
  11 +
  12 + # TODO: use noisebridge url
  13 + > git clone git://github.com/eleddy/flask-noiselist.git
9 14 > cd flask-noiselist
10 15 > python bootstrap.py
11 16 > ./bin/buildout
12 17
13 18 To get the server running in foreground mode, do::
  19 +
14 20 > bin/flask-ctl debug fg
15 21
16 22 Your app will be running at http://127.0.0.1:5000 with a simple hello world
@@ -18,15 +24,80 @@ placeholder.
18 24
19 25 Take a minute to notice the differences between this app and web2py. There is
20 26 no admin console and no formatting by default. Flask is really a micro framwork.
  27 +
21 28 Notice as well that starting we are in foreground more, and that you don't
22   -have to kill a process or terminal to restart. Simply Ctl-C.
  29 +have to kill a process or terminal to restart. Simply Ctl-C to restart.
  30 +
  31 +Modifying These Instructions
  32 +----------------------------
  33 +Since this is written by a human, please feel free to update the instructions in this
  34 +file itself and commit back. If you need permissions, feel free to contact me or just
  35 +fork and I will merge back. If there is interest in class we will discuss how this
  36 +works with github.
  37 +
  38 +
  39 +Folder Structure
  40 +----------------
  41 +Unlike web2py, you can not edit the application through the web (TTW). All the files
  42 +you will be editing will be in flask-noiselist/src. Take note that the app itself
  43 +should be in egg format.
  44 +
  45 +script.py has a bunch of stuff you don't need to worry about in the moment. This
  46 +basically just sets up the app for running and testing. You should never have to
  47 +modify this.
  48 +
  49 +The only python file we will be working with is __init__.py. Because the app we are
  50 +doing is small, this will be sufficient for all of our code, although it is not
  51 +really considered to be best practice.
  52 +
  53 +Templating
  54 +----------
  55 +Note that unlike web2py, you are presented with a completely blank slate. We will be
  56 +touching a few different files in this exercize than the previous one because of this.
  57 +Let's get a few pretty things ironed out first so we know what we want the end result
  58 +to look like.
  59 +
  60 +First, notice that we don't have any templates yet. Flask uses Jinja2, a "standalone"
  61 +templating engine. Most modern frameworks should be integrating with 1 or more different
  62 +templating engines. This is beneficial to you because you can learn one framework and
  63 +not have to learn a new templating language every time (web2py has its own templating
  64 +language). Jinja2 is very popular and widely used (and more importantly that means it
  65 +is well supported).
  66 +
  67 +First we need to create a directory for holding our templates::
  68 +
  69 + > mkdir templates
  70 +
  71 +And lets take our Hello page and make it into a template by adding a template::
  72 +
  73 + > touch hello.html
  74 +
  75 +In that file, let's add a few lines to show how our list will look in the end::
  76 +
  77 + XXX: Link to that changeset
  78 +
  79 +And then in __init__.py, we will connect the index page with that tempalte by
  80 +adding a decorator::
  81 +
  82 + from flask import render_template
23 83
  84 + ...
  85 +
  86 + @app.route('/')
  87 + def index():
  88 + return render_template('hello.html')
24 89
25 90
  91 +Deployment
  92 +----------
  93 +Flask is run under WSGI. We will discuss what that means in other classes, but
  94 +more importantly you just need to know that routing occurs outside of the app itself***
26 95
27 96
28 97
29 98
30 99 More Info
31 100 ---------
32   -For more info on this buildout itself, please see http://flask.pocoo.org/snippets/27/
  101 + * Flask Documentation: http://flask.pocoo.org/docs/
  102 + * About Jinja2: http://jinja.pocoo.org/docs/
  103 + * For more info on this buildout itself, please see http://flask.pocoo.org/snippets/27/
6 src/noiselist/__init__.py
... ... @@ -1,5 +1,6 @@
1 1 # -*- coding: utf-8 -*-
2 2 from flask import Flask, request
  3 +from flask import render_template
3 4
4 5
5 6 class _DefaultSettings(object):
@@ -21,6 +22,5 @@ def init_db():
21 22
22 23 @app.route('/')
23 24 def index():
24   - if request.args:
25   - BREAK (with_NameError)
26   - return 'Hello %s!' % app.config['USERNAME'].title()
  25 + return render_template('hello.html')
  26 +
18 src/noiselist/templates/hello.html
... ... @@ -0,0 +1,18 @@
  1 +<!DOCTYPE html>
  2 +<html>
  3 + <head>
  4 + <title>TODO at Noisebridge</title>
  5 + </head>
  6 + <body>
  7 + <h1>My Personal TODO List</h1>
  8 + <ul>
  9 + <li>Finish evaluating pull requests</li>
  10 + <li>Finish writing up class work</li>
  11 + <li>Swim and enjoy the sun</li>
  12 + <ul>
  13 + <form action="" method="POST" id="add_to_todo_list">
  14 + <input type="text" name="todo_item"/>
  15 + <input type="submit" name="add_todo_submit" value="Add to List!"/>
  16 + </form>
  17 + </body>
  18 +</html>

0 comments on commit 274814e

Please sign in to comment.
Something went wrong with that request. Please try again.