我的烧瓶应用程序中有几个视图。并且每个视图都有自己的子视图,每个子视图都有不断的小模板。视觉演示和网址结构如下:
|-settings ___________ [@app.route(/manage)]
|--menu_settings _____ [@app.route(/manage?current=menu_settings)]
|--main_settings _____ [@app.route(/manage?current=main_settings)]
|--additional_settings [@app.route(/manage?current=additional_settings)]
|-online_store [@app.route(/online_store)]
|--delivery __ [@app.route(/online_store?current=delivery)]
|--payment ___ [@app.route(/online_store?current=payment)]
|--store _____ [@app.route(/online_store?current=store)]
我想要做的是使用if或switch条件。例如:
@admin.route('/manage', methods = ['GET', 'POST'])
@login_required
def manage(current = ''):
current = request.args.get('current')
if current == 'menu_settings':
return render_template('admin/manage/site_figuration.html',
title = 'Site figuration',
current = current)
elif current == 'internet_market':
return render_template('admin/manage/internet_market.html',
title = 'Internet market',
current = current)
else:
return '404 - Template not found'
我的问题是我做得对吗?或者还有其他更简单的方法可以解决这个问题吗?使用此方法是否合理,或者我必须使用JQuery load()方法?
答案 0 :(得分:3)
看起来你的事情太复杂了。怎么样:
@admin.route('/manage')
@admin.route('/manage/<current>')
@login_required
def manage(current = None):
if current == 'menu_settings':
template = 'admin/manage/site_configuration.html'
title = 'Site configuration'
elif current == 'internet_market':
template = 'admin/manage/internet_market.html'
title = 'Internet market'
elif current is None:
template = 'admin/manage/default.html'
title = 'Default page for manage'
else:
flask.abort(404)
return render_template(template, title=title, current=current)
但我也发现有多条路线作为更正确的方法。为什么重新发明轮子?
@admin.route('/manage')
@login_required
def manage():
return render_template('admin/manage/default.html',
title='Default page for manage',
current=None)
@admin.route('/manage/menu_settings')
@login_required
def manage_menu_settings():
return render_template('admin/manage/site_configuration.html',
title='Site configuration',
current='menu_settings')
@admin.route('/manage/internet_market')
@login_required
def manage_internet_market():
return render_template('admin/manage/internet_market.html',
title='Internet market',
current='internet_market')
注意:请注意,此代码未经测试,请注意错误和/或错别字。
答案 1 :(得分:2)
不,我不认为你正确地把它弄好了。
为什么不创建多个视图?
@admin.route('/manage/menu', methods = ['GET', 'POST'])
@login_required
def manage_menu():
return render_template('admin/manage/site_figuration.html',
title = 'Site figuration',
current = request.args.get('current') )
@admin.route('/manage/internet_market', methods = ['GET', 'POST'])
@login_required
def manage_market():
return render_template('admin/manage/internet_market.html',
title = 'Internet market',
current = request.args.get('current') )
这种方法更具可扩展性/可维护性。
如果你真的想要你可以将所有这些包装成装饰器或基于类的视图(如果flask支持那些)。
修改强>
我主要在金字塔而不是Flask工作(老实说,你可能会考虑切换,因为它类似但整体更强大。)
在金字塔中,我会使用基于类的视图,看起来像这样。
@view_defaults(permission='admin')
class admin_views:
def __init__(self, request):
self.request = request
# Assign anything else you want to precalculate
# for all views to a self variable
@view("manage_menu", renderer='manage_menu.jinja2')
def menu(self):
return {'title':'Manage Menu'} #These are your template variables
@view("internet_market", renderer='internet_market.jinja2')
def market(self):
return {'title': 'Internet Market"}
然后您的路线设置将是:
config.add_route('manage_menu', '/manage/menu')
config.add_route('internet_market', '/manage/market')
当你开始使用参数
时,真正强大的地方# Now the request.params dict will have a key of 'product'
# with whatever that path segment is.
config.add_route('product_view','/products/{product}')
您还可以轻松地使用
生成链接route_path('view_name', request_obj, #any variables here)
e.g。
# That'll generate a proper link.
# If the client is using SSL it'll be an https link, etc.
route_path('product_view', self.request, product=5).