Multiple Url Segment In Flask And Other Python Frameowrks
Solution 1:
I'm fairly new to Flask myself, but from what I've worked out so far, I'm pretty sure that the idea is that you have lots of small route/view methods, rather than one massive great switching beast.
For example, if you have urls like this:
http://example.com/unit/57/http://example.com/unit/57/page/23/http://example.com/unit/57/page/23/edit
You would route it like this:
@app.route('/unit/<int:unit_number>/')
def display_unit(unit_number):
...
@app.route('/unit/<int:unit_number>/page/<int:page_number>/')
def display_page(unit_number, page_number):
...
@app.route('/unit/<int:unit_number>/page/<int:page_number>/edit')
def page_editor(unit_number, page_number):
...
Doing it this way helps to keep some kind of structure in your application and relies on the framework to route stuff, rather than grabbing the URL and doing all the routing yourself. You could then also make use of blueprints to deal with the different functions.
I'll admit though, I'm struggling to think of a situation where you would need a possibly unlimited number of sections in the URL?
Solution 2:
Splitting the string doesn't introduce any inefficiency to your program. Performance-wise, it's a negligible addition to the URL processing done by the framework. It also fits in a single line of code.
@app.route('/<path:fullurl>')defmy_view(fullurl):
params = fullurl.split('/')
Solution 3:
it works:
@app.route("/login/<user>/<password>")deflogin(user, password):
app.logger.error('An error occurred')
app.logger.error(password)
return"user : %s password : %s" % (user, password)
then:
http://example.com:5000/login/jack/hi
output:
user : jack password : hi
Post a Comment for "Multiple Url Segment In Flask And Other Python Frameowrks"