We have the following Build A Character app.

app.py
options.py
[static]
  |__ [css]
  |__ [img]
[templates]
  |__ builder.html
  |__ index.html
  |__ layout.html

Main App Files

[app.py]:

from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route('/')
def index():
  return render_template('index.html')

app.run(debug=True, port=8000, host='0.0.0.0')

[index.html]:

{% extends "layout.html" %}
{% block content %}
...
<form action="" method="POST">
 <label>Name your bear</label>
 <input type="text" name="name" value="" autofocus>
 <input type="submit" value="Let's build it!">
</form>
...
{% endblock %}

Updating Files

In flask if you’re posting values from a form, your route should look like this or you will get the error ‘not allowed’.

[app.py]:

from flask import Flask, render_template, redirect, url_for

@app.route('/save', methods=['POST'])
def save():
  return redirect( url_for('index') )

 

url_for( ) gets the URL of a function, by figuring out its route.
Note: We need to import it to use it in our app, however, this is always available for the use inside the templates.

[index.html]:

<form action="{{ url_for('save') }}" method="POST">

OUTPUT:

http://treehouse-app.com/save     redirects to:   http://treehouse-app.com/

Tracing Data with (Pdb)

To be able to trace requests data, use the power of Pdb

from flask import request
...
@app.route('/save', methods=['POST'])
def save():
  import pdb; pdb.set_trace()
  return redirect( url_for('index') )
...

Run app, and submit the value(s) into the form. Observe data in the console, you’ll find that if you type (Pdb) request.form that data is POSTed in a new data type called ImmutableMultiDict, which is like a Dictionary but immutable.

(Pdb) dir()
(Pdb) dir(request)
(Pdb) request.form 
ImmutableMultiDict([('name', 'Mike')])
(Pdb) c