【问题标题】:How to print list data in next line in Django如何在Django的下一行打印列表数据
【发布时间】:2021-12-17 21:28:28
【问题描述】:

这里我通过 for 循环将一些数据放入列表中,这应该打印在下一行

让我们考虑一下我的views.py

def items_log(request, pk):
    logg = []
    client = request.user.client
    items_log = JobItemsLogs.objects.filter(client=client,item_id=pk).order_by('-id')[:5]
    for x in items_log:
       log_text = 'Type of entry: {0} - date: {1}; Created by: {2}'.format(
                x.type_of_entry,x.created_at.date(),x.created_by)
       logg.append(log_text)
    ...
    ...
    ...

现在让我们将 index.html 视为

<div class="span4">
            <div class="well">
                <ul class="nav nav-list">
                    <li class="nav-header" >Log entries</li>

                       {% for i in logg %}
                       {{i}}
                       {% endfor %}

                </ul>
            </div>
        </div>

这里是如何显示的

我想展示的方式是

Type of entry: Plumbing - date: 2021-11-02; Created by: A Krishna*
Type of entry: Plumbing - date: 2021-11-02; Created by: A Krishna*
Type of entry: None - date: 2021-07-28; Created by: A Krishna*
Type of entry: None - date: 2021-07-28; Created by: A Krishna* 
Type of entry: None - date: 2021-07-28; Created by: A Krishna*

这些列表数据中的每一个都应该在新行中看到

【问题讨论】:

  • 用于列表项,所以它应该是{% for i in logg %}&lt;li&gt; {{i}}&lt;/li&gt; {% endfor %}
  • &lt;div&gt; {{i}} &lt;/div&gt; 或者你可以像{{i}} &lt;br&gt;这样在末尾添加换行符
  • 标签: python django django-models django-views django-templates


    【解决方案1】:

    只需将{{i}} 放在p 标记中! :D

    <p>{{i}}</p>
    

    或者你可以加&lt;br&gt;:

    {{i}}<br>
    

    【讨论】:

      【解决方案2】:

      在行后添加换行符-

      log_text = 'Type of entry: {0} - date: {1}; Created by: {2}<br>'.format(
                      x.type_of_entry,x.created_at.date(),x.created_by)
      

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签