正则匹配 demo
from flask import Flask, send_file
from werkzeug.routing import BaseConverter
class RegexConverter(BaseConverter):
def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map)
self.regex = items[0]
app = Flask(__name__)
app.url_map.converters['regex'] = RegexConverter
@app.route('/<regex(".*"):id>')
def example(id):
return send_file('./index.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
404 url重定向
from flask import redirect, send_from_directory
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('js', path)
@app.errorhandler(404)
def page_not_found(error):
return redirect('/')