-
Notifications
You must be signed in to change notification settings - Fork 8
Commit v0.9
kwmccabe edited this page Apr 17, 2018
·
8 revisions
v0.9 - Generic /page/ route, init /static directory
- +3 -0 [A] web/app/main/templates/index.html
- +17 -5 [M] web/app/main/views.py
- +5 -0 [A] web/app/static/css/flaskapp.css
- +7 -0 [A] web/app/static/js/flaskapp.js
- +0 -0 [A] web/app/static/media/dot_blank.gif
- +1 -1 [M] web/app/templates/hello.html
- +2 -2 [M] web/flaskapp.py
- +1 -0 [M] web/requirements.txt
- New, default template for
main
module. - Available at
/
or/home
or/index
.
+<h1>{{ page_title }}</h1>
+<p>template = /web/app/main/templates/index.html</p>
- Replace route
/main/
andfunctionhello_main()
. - Create route
/home/
with a redirect to the default route/
. - Create route
/<page>/
to serve any/templates/
file. For example,/index
renders theindex.html
template. - Wrap
render_template()
within atry...except
block forTemplateNotFound
exceptions.
import logging
import os
-from flask import current_app, escape
+from flask import abort, current_app, escape, redirect, render_template, url_for
+from jinja2 import TemplateNotFound
from . import main
-@main.route('/main/')
-def hello_main():
- logging.info("hello_main()")
- return 'Hello FlaskApp : Main Module'
+@main.route('/home/')
+def main_home():
+ return redirect(url_for('.main_page'))
+
+
+@main.route('/', defaults={'page': 'index'})
+@main.route('/<page>/')
+def main_page(page):
+ try:
+ logging.debug( "main_page( %s )" % page )
+ title = "FlaskApp Page"
+ return render_template('%s.html' % (page), page_title=title)
+ except TemplateNotFound:
+ logging.info('TemplateNotFound: %s.html' % (page))
+ abort(404)
- Minimal project CSS file to initialize the
/app/static/css/
directory.
+@charset "utf-8";
+
+/* set a temporary body color to confirm this css is included */
+body { background-color: #f5f5f5; }
+
- Minimal project JS file to initialize the
/app/static/js/
directory.
+/**
+ * util method
+ * @see somepage.html
+ */
+function doTest( param ) {
+ alert("doTest("+param+")");
+}
- Minimal image resource to initialize the
/app/static/media/
directory.
<h1>{{ page_title }}</h1>
-<p>template = hello.html</p>
+<p>template = /web/app/templates/hello.html</p>
-@app.route('/')
+@app.route('/hello_flaskapp')
...
-@app.route('/debug')
+@app.route('/hello_debug')
Flask==0.12.2
gunicorn==19.7.1
+Jinja2==2.9.6
Commit-v0.8 | Commit-v0.9 | Commit-v0.10
- FlaskApp Tutorial
- Table of Contents
- About
- Application Setup
- Modules, Templates, and Layouts
- Database Items, Forms, and CRUD
- List Filter, Sort, and Paginate
- Users and Login
- Database Relationships
- API Module, HTTPAuth and JSON
- Refactoring User Roles and Item Status
- AJAX and Public Pages