【问题标题】:flask profile page for users用户的烧瓶个人资料页面
【发布时间】:2018-11-05 19:33:12
【问题描述】:

我有一个包含不同客户(房东、租户)的门户

他们有一个注册页面。当他们注册时,我会使用角色适当地标记他们。

当他们登录时,他们要做的第一件事就是填写他们的个人资料。为此,我创建了一个页面 profile.html...

除了少数之外,这两个用户的字段几乎相似。我对房东有一些属性,对租户有一些属性。但是它们都有一些相似的字段,例如 first_name、last_name、phone、age、sex 等......

目前,我维护着两个不同的配置文件表和一个 profile.html 页面。

我将它们发送到 profile.html 并且我正在使用

{% 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 变得太长......

有办法简化吗?

【问题讨论】:

    标签: flask flask-sqlalchemy flask-security


    【解决方案1】:

    此类情况的常用方法是使用template inheritance,它将公共部分分离为“基本”模板。例如,

    <html>
    ...
    <body>
    {% block content %}{% endblock %}
    </body>
    </html>
    

    并通过为您的每个条件提供特定内容的模板继承此基础。例如,为填写个人资料的房东提供内容的模板看起来像

    {% extends "base.html" %}
    {% block content %}
    read only profile pages for landlord
    {% endblock %}
    

    然后通过在视图方法中移动适当的检查来选择适当的模板。类似的东西

    @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 ...
    

    【讨论】:

      猜你喜欢
      • 2020-01-23
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 2016-06-26
      • 2015-03-06
      • 2011-01-25
      相关资源
      最近更新 更多