1

I'm trying to make a program with Flask that let's you quickly create pages. Basically I want to make it so I can have slashes (/) in dynamic URLs e.g. the route is <page> and I enter localhost:5000/test/page, I want it to go to <page> with the <page> being test/page. Is this possible?

Vishnu Dasu
  • 533
  • 5
  • 18
Bevan Ellis
  • 21
  • 1
  • 5

1 Answers1

4

From the Flask documentation on URL variables:

from flask import Flask


app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello, World!\n'

@app.route('/path/<path:subpath>')
def show_subpath(subpath):
    return 'Subpath %s\n' % subpath

Example requests from the command line:

$ curl http://127.0.0.1:5000
Hello, World!
$ curl http://127.0.0.1:5000/path/test/page
Subpath test/page

If you wanted to do something similar with regular expressions, the common solution seems to be adding a regex 'converter', but the path URL variable appears to exist for exactly what you're asking about.

risahbhc32
  • 135
  • 2
  • 12
kungphu
  • 4,592
  • 3
  • 28
  • 37