Django Modal with multiple pictures - django

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

Related

How can I remove a particular item using a popup?

I have many items and I want to delete one of them but when I delete one item it turns out that it deletes the last item which exists based on ordering so, if I have 10 records of id which start from 1 to 10 record so, it will remove the item number 10 til if I remove item number 5 will remove the number 10. this case occurs because of popup but if I remove popup and delete the items directly it will remove with no mistake so, How can I remove a particular item using popup?
profile.html
{% if request.user == profile.user %}
<div class="col-lg-7 offset-lg-1 col-12">
{% if profile.user.user_history.all.count != 0 %}
<form method="post">
{% csrf_token %}
<div class="clear_all fl-left">
<input class="global_checkbox" type="checkbox" name="remove_all_history" id="remove_all_history">
<label for="remove_all_history" class="ml">Remove All</label>
</div>
<div class="fl-right">
<input type="submit" value="Remove" class="clear_button btn btn-danger invisible"/>
</div>
</form>
{% else %}
<p class="text-center">you have no history yet!</p>
{% endif %}
<div class="clearfix"></div>
<div class="mt-6"></div>
{% for history in history_pages %}
{% if history.deleted_history == False %}
<div class="history">
<div class="row">
<div class="col-4">
<form method="post">
{% csrf_token %}
<input class="global_checkbox" type="checkbox" name="remove_one_history" id="remove_all_history">
<span class="ml-2">{{ history.history_time|time }}</span>
<div class="ml ml-4">{{ history.history_time|date:'d M Y' }}</div>
</form>
</div>
<div class="history-content col-7">
<p><strong>text:</strong> {{ history.history }}</p>
<p><strong>action:</strong> {{ history.action_option }}</p>
<p><strong>position:</strong>
{% if history.verb_option == "" %}
POS
{% else %}
{{ history.verb_option }}
{% endif %}
</p>
</div>
<form method="post" action="{% url 'accounts:remove_history' history.id %}">
{% csrf_token %}
<div class="history-list col-1">
<span class="fa fa-ellipsis-v" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
<div class="dropdown-menu">
<a type="button" class="dropdown-item" data-toggle="modal" data-target="#exampleModal">Remove this item</a>
</div>
</div>
{% include 'accounts/popup.html' %}
</form>
</div>
</div>
{% endif %}
{% endfor %}
</div>
{% endif %}
popup.html
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Warning!!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Do you want to remove this history item?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-outline-danger">Remove</button>
</div>
</div>
</div>
</div>
views.py
#login_required
def userProfile(request, slug=None):
profile = None
try:
profile = Profile.objects.get(user__slug=slug)
paginator = Paginator(profile.user.user_history.all(), 100)
page_number = request.GET.get('page_number')
history_pages = paginator.get_page(page_number)
except:
return redirect('accounts:index_404')
return render(request, 'accounts/profile.html', {'profile': profile, 'history_pages': history_pages})
def remove_history(request, id=None):
if id and id is not None:
# History.objects.get(id=id)
print(id)
return redirect("accounts:profile", request.user.username)
Note: I tested the delete using print(id)
In your current code you have included popup.html mutliple times so when you click on a tag its not confirm which modal will get open has all are triggering exampleModal i.e :data-target="#exampleModal" .
So , to overcome this one way would be including only one modal and adding form tags around submit button . Then , whenever user click on a tag you can get action value from form where a tag has been clicked and then add this action value inside modal form tag .
Demo Code :
//on click of `a` tag
$(".dropdown-item").on("click", function() {
//get closest form from `a` tag then get action from it
var action_ = $(this).closest("form").attr("action");
$("#exampleModal form").attr("action", action_) //add that action inside modal form tag
console.log(action_)
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="history">
<div class="">
<div class="">
<form method="post">
{% csrf_token %}
<input class="global_checkbox" type="checkbox" name="remove_one_history" id="remove_all_history">
<span class="">12:30</span>
<div class="">12-04-21</div>
</form>
</div>
<div class="history-content">
<p><strong>text:</strong> Somethigs</p>
<p><strong>action:</strong>Ok</p>
<p><strong>position:</strong> POS
</p>
</div>
<form method="post" action="{% url 'accounts:remove_history' 1 %}">
<div class="history-list">
<span class="fa fa-ellipsis-v" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
<div class="dropdown-menu">
<a type="button" class="dropdown-item" data-toggle="modal" data-target="#exampleModal">Remove this item</a>
</div>
</div>
<!--remove this line {% include 'accounts/popup.html' %}-->
</form>
</div>
</div>
<div class="history">
<div class="">
<div class="">
<form method="post">
{% csrf_token %}
<input class="global_checkbox" type="checkbox" name="remove_one_history" id="remove_all_history">
<span class="">12:32</span>
<div class="">22-04-21</div>
</form>
</div>
<div class="history-content">
<p><strong>text:</strong> Somethigs2</p>
<p><strong>action:</strong>Ok2</p>
<p><strong>position:</strong> POS
</p>
</div>
<form method="post" action="{% url 'accounts:remove_history' 2 %}">
<div class="history-list">
<span class="fa fa-ellipsis-v" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
<div class="dropdown-menu">
<a type="button" class="dropdown-item" data-toggle="modal" data-target="#exampleModal">Remove this item</a>
</div>
</div>
<!--remove this line {% include 'accounts/popup.html' %}-->
</form>
</div>
</div>
<!--just use only one modal no need to include it every time on your page-->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Warning!!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<!--added form tag-->
<form method="post" action="">
<!--added csrf token-->
{% csrf_token %}
<div class="modal-body">
Do you want to remove this history item?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-outline-danger">Remove</button>
</div>
</form>
</div>
</div>
</div>

The best way to divide card-deck into rows in Django template - Django

I want to show 6 items in a page with the same design, like this photo: https://pasteboard.co/Jrkehgg.png.
I have this code in a HTML page:
{% for android in single_android_post %}
<div class="card-deck">
<div class="card">
<img class="card-img-top" src="{{ android.get_image }}" height="240px" width="230px" alt="اذا رأيت هذه الجمله فيرجى التواصل معنا واخبارنا بمكان الخطا لاصلاحه">
<div class="card-body">
<h5 class="card-title">{{ android.name }}</h5>
<p class="card-text"> {{ android.app_contect}} </p>
</div>
<div class="card-footer">
<small class="text-muted">{{ android.post_date}}</small>
<small class="firstsmall"><a class="bg-orange" href="" title="">{{ android.post_tag}}</a></small>
</div>
</div>
</div>
{% endfor %}
I tried this code but each card takes up all of the page width. How do I fix it?
You can do:
<div class="card-deck">
{% for android in single_android_post %}
<div class="card">
<img class="card-img-top" src="{{ android.get_image }}" height="240px" width="230px" alt="اذا رأيت هذه الجمله فيرجى التواصل معنا واخبارنا بمكان الخطا لاصلاحه">
<div class="card-body">
<h5 class="card-title">{{ android.name }}</h5>
<p class="card-text"> {{ android.app_contect}} </p>
</div>
<div class="card-footer">
<small class="text-muted">{{ android.post_date}}</small>
<small class="firstsmall"><a class="bg-orange" href="" title="">{{ android.post_tag}}</a></small>
</div>
</div>
{% if forloop.counter|divisibleby:3 %}
</div>
<div class="card-deck">
{% endif %}
{% endfor %}
</div>
See: forloopcounter and divisibleby

data id iteration django

I really do not know how to iterate through my loop to display individual items. I have tried to
rearrange the loop but has worked. The data id id="portfolioModal" is what I want to also iterate for individual items.
{% for subjects in object %}
<div class="portfolio-modal modal fade" id="portfolioModal" tabindex="-1" role="dialog" aria-labelledby="portfolioModal1Label" aria-hidden="true">
<div class="modal-dialog modal-xl" role="document">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">
<i class="fas fa-times"></i>
</span>
</button>
<div class="modal-body text-center">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-8">
<!-- Portfolio Modal - Title -->
<h2 class="portfolio-modal-title text-success text-uppercase mb-0">{{subjects.title}}</h2>
<!-- Icon Divider -->
<div class="divider-custom">
<div class="divider-custom-line"></div>
<div class="divider-custom-icon">
<i class="fas fa-star"></i>
</div>
<div class="divider-custom-line"></div>
</div>
<!-- Portfolio Modal - Image -->
<img class="img-fluid rounded-circle mb-5" src=" {{subjects.img.url}}" alt="">
<!-- Portfolio Modal - Text -->
<p>{{subjects.desc}}</p>
<button class="btn btn-danger" href="#" data-dismiss="modal">
<i class="fas fa-times fa-fw"></i>
Close Window
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
If you want to iterate just numbers in the loop, you can use the {{ forloop.counter }}, like this:
{% for subjects in object %}
<div class="portfolio-modal modal fade" id="portfolioModal-{{ forloop.counter }}" tabindex="-1" role="dialog" aria-labelledby="portfolioModal1Label" aria-hidden="true">
...
{% endfor %}
In the for loop inside your template code, ever single object of your the context "object" variable is named "subjects", like you know. So, If you want iterate your div id using a object attribute (suppose object.id), just call it by {{ subjects.id }}:
{% for subjects in object %}
<div class="portfolio-modal modal fade" id="portfolioModal-{{ subjects.id }}" tabindex="-1" role="dialog" aria-labelledby="portfolioModal1Label" aria-hidden="true">
...
{% endfor %}
When this page is rendered,this will print a simple id=portfolioModal-1, id=portfolioModal-2, id=portfolioModal-3... where numbers are the objects ids

how to run a function from a link within popup modal bootstrap

I have a django project that includes a popup modal. The modal has tabs as links. When the user clicks on one of the tabs, it must perform a function and retrieve data from the database.
The problem is that once the user clicked on the tab nothing happened, and the function isn't initialized.
urls.py
path("events/<int:id>/", getEvents, name = "getEvents"),
views.py
def getEvents(request,id):
q1 = cursor.execute('SELECT Person_.ID FROM Person_ WHERE Person_.ID = {0}'.format(id))
print('q1==>',q1)
qresult = q1.fetchone()
print("qresult==>",qresult)
print("query is not empty!")
eventQ = cursor.execute('SELECT Person_.ID, Person_.Full_Name, Events.ID, Events.Event_Title, Events.Event_Details, Links.document_id from Person_, Events, Links where ( Person_.ID = {0}) and (Person_.ID = Links.A_Person or Person_.ID = Links.B_Person) and (Events.ID = Links.A_Event or Events.ID = Links.B_Event) ORDER BY Events.ID DESC '.format(id))
resultEvents = eventQ.fetchall()
return render(request,'pictureCard.html',{
"resultEvents":resultEvents,
"qresult":qresult,
})
pictureCrads.html
<!-- Popup Modal -->
{% for obj in object_list %}
<div id="popModel{{obj.0}}" class="modal fade modal-rtl" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header Modal-Pic">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">{{ obj.1 }}</h4>
<img src="{% static '/img/card/1.png'%}"/>
</div>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact{{ obj.0 }}" role="tab" aria-controls="contact" aria-selected="false">contact us</a>
</li>
<li class="nav-item">
<a class="nav-link" id="event-tab" data-toggle="tab" href="{% url 'getEvents' obj.0 %}" role="tab" aria-controls="event" aria-selected="false">Events</a>
</li>
<li class="nav-item mr-auto btn-warning">
<a class="nav-link text-dark" id="contact-tab" data-toggle="tab" href="#picture" role="tab" aria-controls="contact" aria-selected="false">Picture</a>
</li>
</ul>
<div class="modal-body tab-content" id="myTabContent">
<div class="tab-pane fade" id="contact{{ obj.0 }}" role="tabpanel" aria-labelledby="contact-tab">{{ obj.9 }}</div>
{% for event in resultEvents %}
<div class="tab-pane fade" id="event{{ obj.0 }}" role="tabpanel" aria-labelledby="event-tab">
<button class="btn btn-primary" > events</button>
{{ event.2 }}
</div>
{% endfor %}
<div class="tab-pane fade" id="picture" role="tabpanel" aria-labelledby="contact-tab">
<a class="" href="#"><img class="" src="{% static '/img/card/1.png'%}"/></a>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
......
<footer class="card-footer card-footer-buttom text-center bg-info">
<form method="GET" action="{% url 'getEvents' obj.0 %}">
detais
</form>
</footer>
Probably django does not know {% for obj in object_list %}. Because you are sending 2 variables which are resultEvents and qresult, but your html file is looking for 'object_list' dictionary or list. This might be the problem.
Hope this helps,

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