r/pythonhelp • u/2qeb • Jan 06 '22
Flask : Iterate through the templates folder and assign route to each one of them automatically
-1
I am making a link shortener and I write a html file to the templates folder and I have to route to the file automaticly, Here's what I have tried.
The Code
from flask import Flask from flask import render_template import random import string import glob, os app = Flask(__name__) def create_link(page): linkparam = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8)) print(f"Creating link on https://l-nqs.herokuapp.com/{linkparam}") f = open(f'C:/Users/xpyth/OneDrive/Documents/GitHub/l-nqs/templates/{linkparam}.html', 'x') with open(f'C:/Users/xpyth/OneDrive/Documents/GitHub/l-nqs/templates/{linkparam}.html', 'w+') as linkhtml : markup = f''' <!DOCTYPE html> <html> <head> <title>Redirecting...</title> </head> <body> <script> window.location.replace("https://{page}"); </script> </body> </html> ''' linkhtml.write(markup) @app.route(f'/{linkparam}') def linkshortened(): return render_template(f'{linkparam}.html') os.chdir("C:/Users/xpyth/OneDrive/Documents/GitHub/l-nqs/templates") file = glob.glob("*.html") fileWithoutExtention = [item.replace(".html", "") for item in file] print(file) print(fileWithoutExtention) for i in range(len(file) - 1) : print(f'/{fileWithoutExtention[i]}') @app.route(f'/{fileWithoutExtention[i]}') def linkshortenedp(): return render_template(f'{file[i]}') @app.route('/') def default_route(): return render_template('index.html') @app.errorhandler(404) def not_found(e): return render_template('404.html'), 404 @app.route('/create') def create(): create_link("www.google.com") return render_template('about.html') @app.route('/join') def join(): return render_template('join.html') if __name__ == '__main__': app.run() The error I am getting in python upon running my script:
C:\Users\xpyth\OneDrive\Documents\GitHub\l-nqs\venv\Scripts\python.exe C:/Users/xpyth/OneDrive/Documents/GitHub/l-nqs/app.py Traceback (most recent call last): File "C:\Users\xpyth\OneDrive\Documents\GitHub\l-nqs\app.py", line 43, in <module> def linkshortenedp(): File "C:\Users\xpyth\OneDrive\Documents\GitHub\l-nqs\venv\lib\site-packages\flask\scaffold.py", line 439, in decorator self.add_url_rule(rule, endpoint, f, **options) File "C:\Users\xpyth\OneDrive\Documents\GitHub\l-nqs\venv\lib\site-packages\flask\scaffold.py", line 57, in wrapper_func return f(self, *args, **kwargs) File "C:\Users\xpyth\OneDrive\Documents\GitHub\l-nqs\venv\lib\site-packages\flask\app.py", line 1090, in add_url_rule raise AssertionError( AssertionError: View function mapping is overwriting an existing endpoint function: linkshortenedp ['404.html', 'about.html', 'index.html', 'join.html', 'layout.html', 'Z5NKFQJ3.html'] ['404', 'about', 'index', 'join', 'layout', 'Z5NKFQJ3'] /404 /about Process finished with exit code 1
0
Upvotes
1
u/ARRgentum Jan 25 '22
maybe if you format your code so that one can actually read it, you might get some answers :)