Created
October 28, 2020 21:52
-
-
Save laundmo/5632137ac4f2bade286af320e663df91 to your computer and use it in GitHub Desktop.
Flask create views at runtime
This file contains hidden or 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
from flask import Flask, request, render_template | |
from flask.views import View | |
app = Flask(__name__) | |
view_list = [ | |
("/main", "main.html.jinja2", "main"), # assumes the templates exist in a ./templates subdir | |
("/main2", "main2.html.jinja2", "main2"), | |
] | |
class GenericView(View): | |
def __init__(self, template_name): | |
self.template_name = template_name | |
def dispatch_request(self): | |
return render_template(self.template_name) | |
for url, template, viewname in view_list: | |
app.add_url_rule(url, view_func=GenericView.as_view(viewname, template_name=template)) | |
if __name__ == '__main__': | |
app.run(debug = True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment