【问题标题】:How to iterate a ListView items to convert in cards如何迭代 ListView 项目以转换为卡片
【发布时间】:2022-01-17 00:46:55
【问题描述】:

我正在编写一个应用程序“破折号”如何成为许多用户的起点。 “应用程序仪表板”。我想转换卡片中的应用程序列表,“组合样式”。

用户登录平台,并在 url .../dash 中打开 dash(django app)。到这里就完美了。

使用通用视图 - ListView - 我们获得将在平台中可用的应用程序列表(如果模型中存在,它已安装并可供用户使用的应用程序)

urls.py:

path('dash/', views.ListView_Dash_Apps.as_view()),

views.py:

class ListView_Dash_Apps(ListView):
    template_name = "dash/ListarAplicaciones.html"
    model = App

而在html模板中,如何迭代object_list的列??? 有了这个我可以迭代行,但不是列,我收到模型中声明的 str 输出。

<ul>
    {% for e in object_list %}
        <li>{{ e }}</li>
    {% endfor %}
</ul>

如果我可以读取列数据并包含在 html 中(应用程序、url_app、图标等)

【问题讨论】:

    标签: python django django-views django-templates


    【解决方案1】:

    在 HTML 中,列是 object_list 的方法。

    在我的例子中,e.appcode、e.url 和 e.description

    <ul>
        {% for e in object_list %}
            <li>{{ e }}</li>
        {% endfor %}
    </ul>
    

    最后:

    {% for e in object_list %}
        <div class="card" style="width: 18rem;">
            <img class="card-img-top" src="" alt="">
            <div class="card-body">
                <h5 class="card-title">{{e.appname}}</h5>
                <p class="card-text">{{e.description}}</p>
                <a href={{ e.url }} class="btn btn-primary">Abrir</a>
            </div>
            </div>
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2019-06-10
      • 2021-07-11
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 2016-12-14
      • 2017-10-09
      • 2021-05-26
      相关资源
      最近更新 更多