Created
November 12, 2012 13:42
-
-
Save tinnet/4059459 to your computer and use it in GitHub Desktop.
flask extension pluggable views routing errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
import flask.views | |
import logging | |
logging.basicConfig(level=logging.DEBUG) | |
class MyFlaskExt(object): | |
def __init__(self, app=None): | |
if app is not None: | |
self.init_app(app) | |
def init_app(self, app): | |
self.blueprint = flask.Blueprint('myext', __name__, static_folder='static', template_folder='templates') | |
self.blueprint.add_url_rule('/', view_func=RootView.as_view('root'), endpoint='root') | |
self.blueprint.add_url_rule('/var/<somevar>', view_func=VarView.as_view('var'), endpoint='var') | |
self.blueprint.add_url_rule('/novar', view_func=NoVarView.as_view('novar'), endpoint='novar') | |
app.register_blueprint(self.blueprint) | |
class RootView(flask.views.View): | |
def dispatch_request(self): | |
logging.debug(flask.url_for('novar')) | |
logging.debug(flask.url_for('novar', _external=True)) | |
return flask.redirect(flask.url_for('var', somevar='test')) | |
class VarView(flask.views.View): | |
def dispatch_request(self, somevar): | |
return "SUCCESS! ({})".format(somevar) | |
class NoVarView(flask.views.View): | |
def dispatch_request(self): | |
return "SUCCESS!" | |
if __name__ == '__main__': | |
app = flask.Flask(__name__) | |
app.debug = True | |
my_ext = MyFlaskExt() | |
my_ext.init_app(app) | |
logging.debug(app.url_map) | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment