Skip to content

Commit v0.9

kwmccabe edited this page Apr 17, 2018 · 8 revisions

v0.9 - Generic /page/ route, init /static directory


Files changed (8)

File web/app/main/templates/index.html ADDED

  • 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>

File web/app/main/views.py MODIFIED

  • Replace route /main/ and functionhello_main().
  • Create route /home/ with a redirect to the default route /.
  • Create route /<page>/ to serve any /templates/ file. For example, /index renders the index.html template.
  • Wrap render_template() within a try...except block for TemplateNotFound 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)

File web/app/static/css/flaskapp.css ADDED

  • 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; }
+

File web/app/static/js/flaskapp.js ADDED

  • Minimal project JS file to initialize the /app/static/js/ directory.
+/**
+ * util method
+ * @see somepage.html
+ */
+function doTest( param ) {
+    alert("doTest("+param+")");
+}

File web/app/static/media/dot_blank.gif ADDED

  • Minimal image resource to initialize the /app/static/media/ directory.

File web/app/templates/hello.html MODIFIED

 <h1>{{ page_title }}</h1>
-<p>template = hello.html</p>
+<p>template = /web/app/templates/hello.html</p>

File web/flaskapp.py MODIFIED

-@app.route('/')
+@app.route('/hello_flaskapp')

...

-@app.route('/debug')
+@app.route('/hello_debug')

File web/requirements.txt MODIFIED

 Flask==0.12.2
 gunicorn==19.7.1
+Jinja2==2.9.6

Commit-v0.8 | Commit-v0.9 | Commit-v0.10

Clone this wiki locally