Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Routing in Flask

Having got "hello world" working on your server in Flask (see [here] if you haven't!), you'll want to add further routes for other URLs - and indeed you'll want those routes to contain match markers so you can set up whole families of URLs. Our first example included:

  @app.route("/")
  def hello():
    etc


and we'll now add some further routes (using the route decorator):

  @app.route("/<word>")
  def greet(word):
    return "This is the page called {0:s} under flask".format(word)


  @app.route("/<folder>/<word>")
  def greeting(folder,word):
    return "This is the page called {0:s} in the {1:s} folder".format(word,folder)


Sample outputs:

 


and you can also include rules such as
  @app.route('/post/<int:post_id>')
which will only match an integer. Also float and path are available - for a floating point number, and for a complete path including "/" which isn't allowed by the default.

The routes above are included in our sample program [here] - which also runs the Flask application with an extra parameter "debug=True"; this replaces the default error messages with something longer and more meaningful ... which will also reveal to a knowledgable user to your site rather more about how the site works if (s)he finds a broken page. You have been warned!

Here's the error that's on the server log (I added a deliberate mistake to cause this):
  192.168.200.42 - - [11/Oct/2015 11:08:09] "GET /arthur HTTP/1.1" 500 -
  Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
      return self.wsgi_app(environ, start_response)
    [snip]
  TypeError: greet() takes exactly 2 arguments (1 given)

and here's the display to the user (this is just the top of the page - scroll down for more!)


The routing and more is base on Werkzeug- http://werkzeug.pocoo.org - and the whole comes together with much more in Full Stack Python - http://www.fullstackpython.com/flask.html.
(written 2015-10-11)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y307 - The Flask micro webdevelopment framework for Python
  [4540] Unittest of a Flask application including forms - (2015-10-15)
  [4538] Flask and unittest - hello web app test world - (2015-10-15)
  [4534] Flask - complete MVC site to navigate around a data set - (2015-10-13)
  [4533] Sessions in flask - (2015-10-13)
  [4532] Flask - first forms and responses with wtforms - (2015-10-12)
  [4531] Jinja2 - Flask templating - (2015-10-11)
  [4530] Requests in Flask - (2015-10-11)
  [4529] Flask - calling in the templating engine - (2015-10-11)
  [4527] Hello Flask world / Python web micro framework - (2015-10-11)


Back to
Hello Flask world / Python web micro framework
Previous and next
or
Horse's mouth home
Forward to
Flask - calling in the templating engine
Some other Articles
Routing in Flask
Day trip to Lancaster
What does Tcl do if you try to run a command that is not defined?
Tcl - a new example for data reformatting
Catching failed commands and not crashing the program in Tcl
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4528_Rou ... Flask.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat