My problem
I am making a shopping mall using Django, Bootstrap
I want to implement technology line break when the post becomes 4
I thought if I used col-3 and {$ for %} {% endfor %} it would be divided into four and line breaks. but not working
How can i fix it?
MY models
JavaScript
x
10
10
1
from django.db import models
2
3
class Cloth(models.Model):
4
title = models.CharField(max_length=30)
5
explain = models.CharField(max_length=30)
6
price = models.IntegerField(blank=True)
7
8
def __str__(self):
9
return self.title
10
My views
JavaScript
1
7
1
from django.shortcuts import render
2
from .models import Cloth
3
4
def index(request):
5
post = Cloth.objects.all()
6
return render(request, 'Blog/index.html', {'post':post})
7
My urls
JavaScript
1
9
1
from django.urls import path
2
3
from . import views
4
5
urlpatterns = [
6
path('', views.index, name='index'),
7
8
]
9
My index.html
JavaScript
1
17
17
1
<div class="container py-3">
2
<h2 align="center"> Top </h2>
3
</div>
4
<div class="container py-2 px-1">
5
<div class="row">
6
{% for p in post %}
7
<div class="card col-3" style="width: 16rem;">
8
<img src="http://placeimg.com/600/600/any" class="card-img-top" alt="...">
9
<div class="card-body">
10
<h5 class="card-title"> {{ p.title }}</h5>
11
<p class="card-text"> {{ p.explain }}</p>
12
<p> {{ p.price }}</p>
13
</div>
14
</div>
15
{% endfor %}
16
</div>
17
Advertisement
Answer
The problem is you are putting everything into a single row div.
You can use divisibleby and forloop.counter to test if the record number is a multiple of four, and, if so, to close your row div and open another in your template. See the docs for further details on these template tools.
Index.html
JavaScript
1
23
23
1
<div class="container py-2 px-1">
2
<div class="row">
3
{% for p in post %}
4
<div class="card col-3" style="width: 16rem;">
5
<img src="http://placeimg.com/600/600/any" class="card-img-top" alt="...">
6
<div class="card-body">
7
<h5 class="card-title"> {{ p.title }}</h5>
8
<p class="card-text"> {{ p.explain }}</p>
9
<p> {{ p.price }}</p>
10
</div>
11
</div>
12
13
14
{% if forloop.counter|divisibleby:"4" %}
15
<!-- here we close the row and then reopen another -->
16
</div>
17
<div class="row">
18
{% endif %}
19
20
{% endfor %}
21
</div>
22
</div>
23