display data in multiple columns with for loop - django

I'm sure this is a simple answer but for the life of me I cant figure / find out how to go about this.
I want to display blog posts from my data base in three equal columns.
When I start looking into the bootstrap docs this is the code given.
<div class="container">
<div class="row">
<div class="col-sm">
One of three columns
</div>
<div class="col-sm">
One of three columns
</div>
<div class="col-sm">
One of three columns
</div>
</div>
</div>
However If I go to implement it I dont understand how i would be able to put the for loop on it without it repeating everything 3 times. IE:
{% for post in posts.all %}
<div class="row">
<div class="col-sm">
<img class="post-image" src="{{ post.image.url }}" />
</div>
<div class="col-sm">
<img class="post-image" src="{{ post.image.url }}" />
</div>
<div class="col-sm">
<img class="post-image" src="{{ post.image.url }}" />
</div>
</div>
{% endfor %}
If you could please point me in the right direction on how to go about this it would be very much appreciated bootstrap is not a requirement.

The view should "prepare" the data to make it easy to enumerate over this. We can create chunks with:
def some_view(request):
posts = list(Post.objects.all())
posts = [posts[i:i+3] for i in range(0, len(posts), 3)]
return render(request, 'some-template.html', {'products': products})
in the template we can then work with a double {% for … %}…{% endfor %} loop [Django-doc]:
{% for chunk in posts %}
<div class="row">
{% for post in chunk %}
<div class="col-sm">
<img class="post-image" src="{{ post.image.url }}" />
</div>
{% endfor %}
</div>
{% endfor %}

Related

Django Calculation inside HTML

I wanted to do something simmilar to this suing django. but somehow i it doesn't work. how do i fix it?
for statik in statistik{
print(statik*total/100)
}
Is there any documentation regarding what I'm trying to implement to my django app? Thank you
Here's the HTML :
{% if statistics %}
{% for statik in statistics|slice:":4" %}
<div class="mb-3">
<div class="small text-gray-500">{{ statik.name }}
<div class="small float-right"><b>{{ statik.voters }} of {{ total }} Voters</b></div>
</div>
<div class="progress" style="height: 12px;">
<div class="progress-bar bg-success" role="progressbar" style="width: {{ statik.voters * total/100 }}%" aria-valuenow="50"
aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>
{% endfor %}
{% else %} <p>END TABLE</p>
{% endif %}
It is recommended you do calculations of any sort in view and pass it in context.
If you still want to go this route there are no math template tags except add included so you should create custom template tags or use django-mathfilters

django slice filter breaks template for tinymce safe content

I have contents which were published using tinymce editor. Now, I want to show my content, that's why I have to use safe filter. But, whenever I'm trying to use slice or truncate filter, the template breaks.
this is my HTML code.
{% extends 'blog/frontend/layouts/base.html' %}
{% block content %}
<div class="card border-primary">
{% for post in posts %}
<div class="card my-2 mx-1">
<div class="card-body">
<h5 class="card-title">{{ post.title }}</h5>
<hr>
<img src="{{ post.image.url }}" alt="Card image" width="85%" class="mx-auto d-block">
<p class="card-text mt-1">
{{ post.content|truncatewords:50|safe}}
</p>
<hr>
<div class="d-flex">
<div class="px-2 py-0 my-auto">
<h6><i class="fas fa-user-edit"></i> {{ post.author.username }}</h6>
</div>
<div class="px-2 py-0 my-auto">
<h6>Date posted: <span class="text-info">{{ post.date_posted|date:"d M, Y" }}</span></h6>
</div>
<div class="ml-auto px-2 my-auto ">
Read more
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
These are the image, first one without sliceor turncate filter, 2nd and 3rd one are with them.
Sorry, it was a stupid question to ask. Just in case anyone else is facing this kind of problem, use truncatechars_html or truncatewords_html just like this: {{ post.content|truncatewords_html:30|safe }} or {{ post.content|truncatechars_html:30|safe }}

using specific bootstrap columns in django template based on the numbers of models

i have created a search result page which results 3 different models. For the results i need to use bootstrap columns. so what i need to do is display them with bootstrap columns.and there's a condition that if there is only one search result i need to use col-12 and if two col-6 and if three col-4
can anyone provide me a demo code?
enter code here
{% if jud %}
<h2>Search results:</h2>
<div class="row">
{% for product in products %}
<div class="col-md-4">
<div class="jumbotron">
<h4>{{ product.title }}</h4>
<p>{{ product.description }}</p>
<h6 class="btn btn-primary btn-large">More info</h6>
</div>
</div>
{% endfor %}
</div>
{% endif %}
You can use this inside row div.
<div class="col"></div>
How it works: If you don't specify the number of columns, all columns in the row take up the same amount of space.
You can write custom template tag.
Something like this:
#register.simple_tag
def column_width(products):
width = {1: 12, 2: 6, 3: 4}
return width[len(products)]
And in template:
{% for product in products %}
<div class="col-md-{% column_width products %}">
<div class="jumbotron">
<h4>{{ product.title }}</h4>
<p>{{ product.description }}</p>
<h6 class="btn btn-primary btn-large">More info</h6>
</div>
</div>
{% endfor %}

target correct image for modal in django template

My template
<div>
<ul>
{% for image in obj.images_set.all %}
<li class="col-lg-4 text-center">
<a data-toggle="modal"data-target="#myModal{{forloop.counter0}}">
<img src="{{ image.image.url }}" alt=""/></a>
</li>
{% endfor %}
</ul>
</div>
{% for image in obj.images_set.all %}
<div id="myModal{{forloop.counter0}}" class="modal fade"
role="dialog">
<div class="modal-dialog" style="text-align:center;">
<div class="modal-content" style="display:inline-block;">
<img src="{{ image.image.url }}" alt=""/>
</div>
</div>
</div>
{% endfor %}
Im having a list of tasks. Im looping over the tasks and an obj represents one task.Each task has multiple images(1,2 or 3).The tasks are listed and the images are displaying correctly.Im trying to show a modal popup when clicking on an image inside a task. Im specifying the target with an id using for loop counter.The problem is that im not able to target the correct image as im having a list of images for each task...SO the for loop goes like 0,1,2 for images in first task and then 0,1,2 also for second task and im not able to assign a unique id for the data-target.How do i overcome this?
<div>
<ul>
{% for image in obj.images_set.all %}
<li class="col-lg-4 text-center">
<a data-toggle="modal"data-target="#myModal{{image.pk}}
{{forloop.counter0}}">
<img src="{{ image.image.url }}" alt=""/></a>
</li>
{% endfor %}
</ul>
</div>
{% for image in obj.images_set.all %}
<div id="myModal{{ image.pk }}{{forloop.counter0}}" class="modal fade"
role="dialog">
<div class="modal-dialog" style="text-align:center;">
<div class="modal-content" style="display:inline-block;">
<img src="{{ image.image.url }}" alt=""/>
</div>
</div>
</div>
{% endfor %}
Resolved the issue.Just added image.pk to the id nad was able to make id unique

Display articles from database in bootstrap columns

I have a large database of articles, I want to display them all in bootstrap rows(3 articles per row):
<div class="container">
<h1 class="mt-4 mb-4 text-center display-4">All News </h1>
<div class="row">
{% for article in articles %}
<div class="col-md">
<div class="card">
<h2 class="pt-2">{{ article.title }}</h2>
<p class="lead"> {{ article.description }}</p>
</div>
</div>
{% endfor %}
</div>
</div>
The problem is that row is created outside the for statement, so every article is squeezed into one row (when there should be 3 articles per row). Is there a way to loop through the for statement, and put 3 articles in one row, close that row and then begin a new one?
You could try to use the value of forloop.counter to change the behavior every three iterations.
<div class="container">
<h1 class="mt-4 mb-4 text-center display-4">All News </h1>
<div class="row">
{% for article in articles %}
{% if forloop.counter|divisibleby:3 %}
<div class="row">
{% endif %}
<div class="col-md">
<div class="card">
<h2 class="pt-2">{{ article.title }}</h2>
<p class="lead"> {{ article.description }}</p>
</div>
</div>
{% if forloop.counter|divisibleby:3 %}
</div>
{% endif %}
{% endfor %}
</div>
</div>