How to add subfolder for url_for path?
Question:
Currently i have blueprint named ‘analytics’ and views.py file in this blueprint folder.
All my functions stored in this single views.py file and i’m calling them with url_for like:
url_for('analytics.get_all_equipped_armors')
url_for('analytics.gamerounds_basic_info')
url_for('analytics.get_average_kill_from_weapons')
I want to separate them logically without creating another blueprints for every entity which exist in my db.
I want to use endpoints for url_for() with subfolder like url_for('analytics.armors.get_all_equipped')
but seems, that it is forbidden to add subfolder for path.
How to organize a structure of blueprints and endpoints with subfolders for url_for function?
blueprint:
from flask import Blueprint
analytics = Blueprint('analytics', __name__, url_prefix='/analytics')
from . import views
function:
@analytics.route('/armors/get-all-equipped', methods=['GET', 'POST'])
def get_all_equipped_armors():
form = LvlRangesForm()
if request.method == 'POST':
if form.validate_on_submit():
min_lvl = form.min_level.data
max_lvl = form.max_level.data
data = Armors.get_all_equipped(min_lvl=int(min_lvl), max_lvl=int(max_lvl))
return render_template('analytics/armors/all_equipped_armors.html', form=form, data=data, result=True)
return render_template('analytics/armors/all_equipped_armors.html', form=form)
Answers:
This may be too simple, but it is not necessary to duplicate all of the route information in the endpoint function name. You can leave your url_for() call just as they are and change only the organization of your template files.
In the alternative, you can name your endpoints as you show in your question, but use underscores to make your logical separators: url_for('analytics.armors_get_all_equipped')
.
Currently i have blueprint named ‘analytics’ and views.py file in this blueprint folder.
All my functions stored in this single views.py file and i’m calling them with url_for like:
url_for('analytics.get_all_equipped_armors')
url_for('analytics.gamerounds_basic_info')
url_for('analytics.get_average_kill_from_weapons')
I want to separate them logically without creating another blueprints for every entity which exist in my db.
I want to use endpoints for url_for() with subfolder like url_for('analytics.armors.get_all_equipped')
but seems, that it is forbidden to add subfolder for path.
How to organize a structure of blueprints and endpoints with subfolders for url_for function?
blueprint:
from flask import Blueprint
analytics = Blueprint('analytics', __name__, url_prefix='/analytics')
from . import views
function:
@analytics.route('/armors/get-all-equipped', methods=['GET', 'POST'])
def get_all_equipped_armors():
form = LvlRangesForm()
if request.method == 'POST':
if form.validate_on_submit():
min_lvl = form.min_level.data
max_lvl = form.max_level.data
data = Armors.get_all_equipped(min_lvl=int(min_lvl), max_lvl=int(max_lvl))
return render_template('analytics/armors/all_equipped_armors.html', form=form, data=data, result=True)
return render_template('analytics/armors/all_equipped_armors.html', form=form)
This may be too simple, but it is not necessary to duplicate all of the route information in the endpoint function name. You can leave your url_for() call just as they are and change only the organization of your template files.
In the alternative, you can name your endpoints as you show in your question, but use underscores to make your logical separators: url_for('analytics.armors_get_all_equipped')
.