target correct image for modal in django template - django

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

Related

How to make data that comes from Django database appear horizontally?

In this Image, the data appears in vertical format. But I want data in horizontal format.
{% block title %}Blog{% endblock title %}
{% block body %}
<div class="container">
<div class="row">
{% for post in posts%}
<div class="col-lg-3">
<div class="card" style="width: 31.25rem;">
<!-- <div class="card"> -->
<img class="img-fluid card-img-top" src="https://images.unsplash.com/photo-1591154669695-5f2a8d20c089?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1887&q=80" alt="Card image cap" style="height: 250px;width: 500px;">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</div>
</div>
</div>
</div>
<div class="post block">
<h2 class="subtitle">{{post.title}}</h2>
<small>Posted At {{post.date_added}}</small>
<p>{{post.intro}}</p>
Read More
</div>
{% endfor %}
{% endblock body %}
Hi There I'm Newbie In Django
How Can I Able To Appear Horizontally The Data Is Come From Database
So The Problem Was I Was Ending Row Inside For Loop To Create New Row You Have To Put For Loop Outside
Check This Code
{% extends 'base.html' %}
{% block title %}Blog{% endblock title %}
{% block body %}
<div class="container">
<div class="row">
{% for post in posts%}
<div class="col-lg-4">
<div class="card" style="width: 31.25rem;">
<!-- <div class="card"> -->
<img class="img-fluid card-img-top" src="https://images.unsplash.com/photo-1591154669695-5f2a8d20c089?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1887&q=80" alt="Card image cap" style="height: 250px;width: 500px;">
<div class="card-body">
<h5 class="card-title">{{post.title}}</h5>
{{post.date_added}}
<p class="card-text">{{post.intro}}</p>
Go somewhere
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock body %}

display data in multiple columns with for loop

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 %}

Posts list page, Django Wagtail

I am making a news site, there is a page where all articles are displayed. But if there are many articles, then the page will be huge.
my code :
{% block content %}
<div class=" container" style="margin-top: 60px;">
{% for post in posts %}
<div class="row" style="padding-top: 30px ">
<div class="col-sm-3">
{% image post.preview_image fill-250x250 as blog_img %}
<img style="border-radius: .3525rem" href="{{ post.url }}" src="{{ blog_img.url }}">
</div>
<div class="col-sm-9">
<a href="{{ post.url }}">
{{ post.title }}
</a>
<br/>
<p> {{ post.intro }}</p>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
what i want to do
I recommend using pagination : https://learnwagtail.com/tutorials/how-to-paginate-your-wagtail-pages/

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 }}

Django Modal with multiple pictures

i'm just trying to make Django display images in modal popup. My problem is, if several images are used, that only the first image is displayed in the modal popup. In the scaled down images that are used as a button, the correct images are displayed. Therefore I do not understand what is wrong here. What should it look like?
{% block content %}
{% if object.image_count %}
<div class="row">
<div class="col-lg-12">
{% for img in object.image_set.all %}
{% thumbnail img.file "150x150" crop="center" as im %}
<!--a href='{{ img.file.url }}' data-lightbox="lightbox[{{ object.id }}]"
title="{{ object.title }}">
<img itemprop="image" src='{{ im.url }}' alt='{{ object.title }}' title='{{ object.title }}'
width="{{ im.width }}" height="{{ im.height }}" class="img-rounded"/>
</a-->
<!-- image trigger modal -->
<a data-toggle="modal" data-target="#myModal">
<img src="{{ im.url }}">
</a>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">{{ object.title }}</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<img itemprop="image" src='{{ img.file.url }}' class="img-rounded" style="width:100%">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
{% endthumbnail %}
{% endfor %}
</div>
</div>
{% endif %}
{% endblock %}
You're creating multiple elements with the same ID attribute in a loop, which means that all thumbnails link to the same modal. Differentiate the IDs with Django's built in loop counter:
<a data-toggle="modal" data-target="#myModal{{ forloop.counter }}">
...
<div class="modal fade" id="myModal{{ forloop.counter }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
will output #myModal1, myModal2 etc. Django docs