如何在Django中将行调整为列?

2024-10-03 21:33:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在index.html和我在views.html中添加的图片中做了一个循环,每当我运行代码时,所有图片都会以行的形式出现。我希望它出现在列中。我怎么能这么做

views.py
    def index(request):
    dest1 = Destination()
    dest1.desc = 'Hello, How are you?'
    dest1.img = '01.jpg'

    dest2 = Destination()
    dest2.desc = 'Hello, HOw are you?'
    dest2.img = '02.jpg'

    dests1 = [dest1, dest2]
    return render(request, 'index.html',{'dests1':dests1})

  index.html
    {% static 'images/fulls' as baseUrl %}
    {% static 'images/thumbs' as hiUrl %}
    {% for dest1 in dests1 %}
    <div>
    <a href="{{baseUrl}}/{{dest1.img}}">
        <img src="{{hiUrl}}/{{dest1.img}}" alt="" />
        <h3>{{dest1.desc}}</h3>
    </a>
   </div>
    {%endfor%}

Tags: youhelloimgindexrequesthtml图片destination
1条回答
网友
1楼 · 发布于 2024-10-03 21:33:50

尝试在代码中添加此项:

<div class="row">
{% for dest1 in dests1 %}
<div class="col-lg-4">
    <a href="{{baseUrl}}/{{dest1.img}}">
        <img src="{{hiUrl}}/{{dest1.img}}" alt="" />
        <h3>{{dest1.desc}}</h3>
    </a>
</div>
{%endfor%}
   </div>

相关问题 更多 >