Flask 用户的烧瓶配置文件页面
我有一个不同客户的门户,房东、房客 有他们的注册页面。当他们注册时,我使用角色适当地标记他们 当他们登录时,他们要做的第一件事就是填写他们的个人资料。为此,我创建了一个页面profile.html 这两个用户几乎都有相似的字段,除了少数字段。我对房东和房客有一些属性。但它们都有一些相似的字段,如名字、姓氏、电话、年龄、性别等 目前,我维护两个不同的profile表和一个profile.html页面 我将它们发送到profile.html,并使用Flask 用户的烧瓶配置文件页面,flask,flask-sqlalchemy,flask-security,Flask,Flask Sqlalchemy,Flask Security,我有一个不同客户的门户,房东、房客 有他们的注册页面。当他们注册时,我使用角色适当地标记他们 当他们登录时,他们要做的第一件事就是填写他们的个人资料。为此,我创建了一个页面profile.html 这两个用户几乎都有相似的字段,除了少数字段。我对房东和房客有一些属性。但它们都有一些相似的字段,如名字、姓氏、电话、年龄、性别等 目前,我维护两个不同的profile表和一个profile.html页面 我将它们发送到profile.html,并使用 {% if user == 'landlord'
{% if user == 'landlord' %}
<html
<body>
profile pagefor landlord
</body>
</html>
{% endif %}
{% if user == 'tenant' %}
<html
<body>
profile pagefor tenant
</body>
</html>
{% endif %}
如果我为每个用户重复整个HTML块,那么上述结构的问题就会出现
一旦用户填写了他们的个人资料,我就会向他们显示只读的profile.html页面,如
{% if user == 'landlord' and profile_filled %}
<html
<body>
read only profile page for landlord
</body>
</html>
{% endif %}
{% if user == 'tenant' and profile_filled %}
<html
<body>
read only profile page for tenant
</body>
</html>
{% endif %}
页面profile.html太长,带有这些IF's
有什么方法可以简化这一点吗?对于这种情况,一种常见的方法是使用,它将公共部分分离到一个基本模板中。例如:
<html>
...
<body>
{% block content %}{% endblock %}
</body>
</html>
然后,通过在视图方法中移动相应的复选框来选择相应的模板。差不多
@app.route('/profile')
def profile():
...
if user == 'landlord' and user.has_filled_in_profile():
return render_template("landlord_with_profile.html", ...)
elif user == 'tenant' and user.has_filled_in_profile():
return render_template("tenant_with_profile.html", ...)
elif ...
@app.route('/profile')
def profile():
...
if user == 'landlord' and user.has_filled_in_profile():
return render_template("landlord_with_profile.html", ...)
elif user == 'tenant' and user.has_filled_in_profile():
return render_template("tenant_with_profile.html", ...)
elif ...