Last active
August 29, 2015 14:21
-
-
Save ssherar/4cfb4d131b55715ec82e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request, g, redirect | |
# Set up the app yo | |
app = Flask(__name__) | |
# Responds to only GET on the root URL | |
# and renders the index.tpl as part of the return | |
# all methods "decorated" witn the @app | |
# /HAVE/ to return something | |
@app.route("/", methods=["GET"]) | |
def home(): | |
return render_template("index.tpl") | |
# Responds to only POST to this url, and | |
# blindly redirects to a privileged page | |
# might want to change that. | |
@app.route("/login", methods=["POST"]) | |
def login(): | |
username = request.form.username | |
password = request.form.password | |
# g stores globals accross the application | |
g.loggedIn = True | |
# arbitary check | |
redirect("/dashboard") | |
return None | |
# We haven't explicitly specified a method, but we | |
# can do what we like - YOLO | |
@app.route("/dashboard") | |
def dashboard(): | |
if not g.loggedIn: | |
redirect("/") | |
return render_template("dashboard.tpl", url_parameter=None) | |
# URL parameters can be specified by angle brackets | |
# and the name is placed as a variable in the method. Cool right? | |
@app.route("/dashboard/<id>") | |
def drilldown(id): | |
return render_template("dashboard.tpl", url_parameter=id) | |
if __name__ == "__main__": | |
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "template.tpl" %} | |
{% block content %} | |
Hello world | |
The URL parameter is : {{ url_parameter }} | |
{% endblock %} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "template.tpl" %} | |
{% block content %} | |
<form method="post" action="/login"> | |
<input type="text" name="username"> | |
<input type="password" name="password"> | |
<input type="submit"> | |
</form> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Nike</title> | |
</head> | |
<body> | |
{% block content %} | |
{% endblock %} | |
</body> | |
<html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment