← flask-blog

get request, post request, forms,

55bc918f27ee4ffeea22a78d57ae46db79d82b3b · ovidiucs · May 13, 2015 12:39

+33 −1 · 2 files changed

  • flask-blog.py +10 −1
  • templates/form.html +23 −0
diff --git a/flask-blog.py b/flask-blog.py
index da5fbcc..efa5a4c 100644
--- a/flask-blog.py
+++ b/flask-blog.py
@@ -1,4 +1,4 @@
-from flask import Flask, render_template
+from flask import Flask, render_template,request
 from flask.ext.bootstrap import Bootstrap
 
 
@@ -39,6 +39,15 @@ def index():
 
     return render_template('weather-average.html', city='Portland, OR',
                            months=months, weather=weather, highlight=highlight)
+
+@app.route('/jin2/form', methods=['GET','POST'])
+def findex():
+    name = None
+    if request.method == 'POST' and 'name' in request.form:
+        name = request.form['name']
+    return render_template('form.html', name=name)
+
+
 @app.errorhandler(404)
 def not_found(e):
     return render_template('404.html')
diff --git a/templates/form.html b/templates/form.html
new file mode 100644
index 0000000..bc82028
--- /dev/null
+++ b/templates/form.html
@@ -0,0 +1,23 @@
+{% extends "bootstrap/base.html" %}
+
+{%  block title %}Form Example{% endblock %}
+
+{% block navbar %}
+    <nav class="navbar navbar-inverse" role="navigation">
+        <div class="container">
+            <a class="navbar-brand" href="{{ url_for('index') }}">Hello</a>
+        </div>
+    </nav>
+{% endblock %}
+
+{% block content %}
+    <div class="container">
+        <form method="POST" action="">
+            What is your name? <input type="text" name="name">
+            <input type="submit" name="submit" value="Submit">
+        </form>
+        {%  if name %}
+            <h1>Hello, {{ name }}!</h1>
+        {% endif %}
+    </div>
+{% endblock %}
\ No newline at end of file