Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,34 @@
Simple "Hello, World" application using Flask
"""

from flask import Flask
from flask import Flask, render_template, request, url_for
app = Flask(__name__)

'''with app.test_request_context('/form', method='POST'):
assert request.path == '/form'
assert request.method == 'POST'
'''
@app.route('/')
def hello_world():
return 'Hello World!'
return render_template('index.html')

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

@app.route('/form', methods=['POST'])
def form():
name = request.form['name']
age = request.form['age']
ninja = request.form['ninja']
if name == '' or age == '' or ninja == '':
return render_template('error.html', error="At least one of the fields was empty!")
else:
try:
int(age)
except ValueError:
return render_template('error.html', error="Age must be an integer")
return render_template('form.html', name=name, age=age, ninja='Patrick Huston')

if __name__ == '__main__':
app.run()
app.run()
15 changes: 15 additions & 0 deletions templates/action.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<title>Form</title>
<h1>Login</h1>
<div>
<form method="post" action="{{ url_for('form') }}">
<label for="name">Please enter your name:</label>
<input type="text" name="name" /><br />
<label for="age">Please enter your age:</label>
<input type="text" name="age" /><br />
<label for="ninja">Please enter your favorite Sofdes Ninja:</label>
<input type="text" name="ninja" /><br />
<input type="submit" />
</form>
</div>
</html>
13 changes: 13 additions & 0 deletions templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>
Error
</title>
</head>
<body>
<h1>Error</h1>
<p>{{ error }}</p>
<h2><a href="/login"><b>login</b></a></h2>
</body>
</html>
11 changes: 11 additions & 0 deletions templates/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<title>Hello from Flask</title>
</head>
<body>
<h1>Hello {{ name }}!</h1>
<h2>Your age is {{ age }}.</h2>
<h2>Your favorite Sofdes ninja is {{ ninja }}.</h2>
</body>
</html>
13 changes: 13 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>
A Small Hello
</title>
</head>
<body>
<h1>Hi</h1>
<p>This is very minimal "hello world" HTML document.</p>
<h2><a href="/login"><b>login</b></a></h2>
</body>
</html>