I have a code in django that creates a carousel for each card item. As I am looping through each image for each specific card, I realized the forloop.counter will keep continuing until the initial for loop has finished. So in my example, i is my initial loop and p is my secondary loop. i loops through cards and p loops through images within the carousel of each card. I need to reset the counter after one i so that the carousel counter starts from the beginning so that first image of every carousel is active.
I'd really appreciate any help
{% for i in inspections %}
<div class="card - mb-3" style="width: 40 rem;">
<div id="myCarousel" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-indicators">
{% for p in photos.all %}
{% if p.inspection_id == i.id %}
<button type="button" data-bs-target="#myCarousel"
data-bs-slide-to="{{ forloop.counter0 }}"
class="{% if forloop.counter0 == 0 %} active {% endif %}"
aria-current="true"
aria-label="Slide {{forloop.counter0}}"></button>
{% endif %}
{% endfor %}
</div>
<div class="carousel-inner">
{% for p in photos.all %}
{% if p.inspection_id == i.id %}
<div class="carousel-item {% if forloop.counter0 == 0 %} active {% endif %}">
<img src="{{ p.InspImages.url }}"
class="img-responsive mx-auto d-block w-80" height="300"
alt="...">
</div>
{% endif %}
{% endfor %}
</div>
<button class="carousel-control-prev" type="button"
data-bs-target="#myCarousel" data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="True"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button"
data-bs-target="#myCarousel" data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="True"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
<div class="card-body">
# body codes #
{% endfor %}
Related
so I'm building a website in Django which have similarities with YouTube. I mean there will be some thumbnails and when you click on one of them it takes you to a page where the video will be played.
Now coming to the code since I don't wanna have to hard-code everything I opted to use the pagination but I have a problem cuz the pagination is doing everything automatically and I can't change the thumbnails for the next page. So either my analysis is wrong or there a way to do it correctly.
Here's the code for the template:
{% load static %}
{% block content %}
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3 mb-5">
<div class="col">
<div class="card shadow-sm">
<a href="{% url 'watch1' %}" target="_blank"> <img src="{% static 'thumbnails/hacker.jpeg' %}" height="225"
width="100%"></a>
<div class="card-body">
<p class="card-text">
{% for element in page_obj %}
{% if element.category == 'boxing' %}
{{ element.description|truncatechars:50 }}
{% endif %}
{% endfor %}
</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col">
<div class="card shadow-sm">
<a href="{% url 'watch2' %}" target="_blank"> <img src="{% static 'thumbnails/hydra.jpg' %}" height="225"
width="100%"></a>
<div class="card-body">
<p class="card-text">
{% for element in page_obj %}
{% if element.category == 'boxing' %}
{{ element.description|truncatechars:50 }}
{% endif %}
{% endfor %}
</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<div class="col">
<div class="card shadow-sm">
<a href="{% url 'watch3' %}" target="_blank"> <img src="{% static 'thumbnails/darkweb.jpg' %}" height="225"
width="100%"></a>
<div class="card-body">
<p class="card-text">
{% for element in page_obj %}
{% if element.category == 'boxing' %}
{{ element.description|truncatechars:50 }}
{% endif %}
{% endfor %}
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
</div>
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
next
last »
{% endif %}
</span>
</div>
{% endblock%}
the function in the view
def boxing(request):
videos = Video.objects.all()
paginator = Paginator(videos, 3) # shows 3 videos per page
pageNumber = request.GET.get('page')
try:
page_obj = paginator.get_page(pageNumber)
except:
page_obj = paginator.get_page(1)
return render(request, 'boxing.html', {'page_obj': page_obj})
And my loop is also displaying the same thing for everything. I could just use an if statement to pick exactly which video I want but with the pagination happening it'll do the same thing on the next page as well.
I've been working on an online portfolio and have been using the Django framework to do so. For a projects tab, I pass in an object (projects) containing the details of all my projects and try to show them in an accordion through a for loop in a dynamic template. But the tabs do not collapse upon clicking them (like they should). I have no idea why.
{% block content %}
<h1>Projects</h1>
<div class="accordion accordion-md" id="projectsAccordion"
role="tablist" aria-multiselectable="true">
{% for project in projects %}
<div class="card">
<div class="card-header" role="tab" id="{{ project.name }}heading">
<a data-toggle="collapse" data-parent="#projectsAccordion"
href="#{{ project.id }}" aria-expanded="false"
aria-controls="{{ project.name }}">
<h5 class="mb-0">
{{ project.name }}
</h5>
</a>
</div> <!-- card header -->
<div id="{{ project.id }}" class="collapse" role="tabpanel"
aria-labelledby="{{ project.name }}heading" data-parent="#projectsAccordion">
<div class="card-block px-3 my-2">
<p>{{ project.description }}</p>
<p>Timeframe: {{project.start_date}} - {{project.end_date}}</p>
{% if project.motivation %}
<p>Motivation: {{ project.motivation }}</p>
{% endif %}
<ul class="list-inline list-unstyled">
{% for tech in project.technologies.all %}
<li class="list-inline-item"><p>{{ tech.name }}</p></li>
{% endfor %}
</ul>
<button type="button" href="{{ project.link }}" target="_blank"
class="btn btn-primary align-text-top project-link">
Code
</button>
</div> <!-- card body -->
</div> <!-- collapse -->
</div><!-- card -->
{% endfor %}
</div> <!-- accordion -->
{% endblock %}```
I want to run the for loop to show all images but the problem is that I have to change the "id" of each image (pic-1 -> pic-2 ...so on) I know how to do it in normal Python but I'm lost on how to do this in template of Django.
<div class="preview-pic tab-content">
<div class="tab-pane active" id="pic-1"><img src="{{product.image.url}}"></div>
{% for image in image_list %}
<div class="tab-pane" id="pic-2"><img src="http://placekitten.com/400/252" /></div>
<div class="tab-pane" id="pic-3"><img src="http://placekitten.com/400/252" /></div>
<div class="tab-pane" id="pic-4"><img src="http://placekitten.com/400/252" /></div>
<div class="tab-pane" id="pic-5"><img src="http://placekitten.com/400/252" /></div>
{% endfor %}
</div>
<ul class="preview-thumbnail nav nav-tabs">
<li class="active"><a data-target="#pic-1" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
{% for image in image_list %}
<li><a data-target="#pic-2" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
<li><a data-target="#pic-3" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
<li><a data-target="#pic-4" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
<li><a data-target="#pic-5" data-toggle="tab"><img src="http://placekitten.com/200/126" /></a></li>
{% endfor %}
</ul>
</div>
You can use {{ forloop.counter }} or {{ forloop.counter0 }}
{% for image in image_list %}
<div class="tab-pane" id="pic-{{ forloop.counter }}"><img src="http://placekitten.com/400/252" /></div>
{% endfor %}
Note that
{{ forloop.counter }} starting index 1
{{ forloop.counter0 }} starting index 0
You can use the forloop.counter to get the loop index:
{% for image in image_list %}
<div class="tab-pane" id="pic-{{ forloop.counter }}"><img src={{ image.url }} />
{% endfor %}
If you have your pic counter starting at 2, you can use the add template tag:
{% for image in image_list %}
<div class="tab-pane" id="pic-={{ forloop.counter|add:1 }}"><img src={{ image.url }} />
{% endfor %}
You can use the {{forloop.counter}} variable. This will return the index of the loop.
forloop.counter The current iteration of the loop (1-indexed)
forloop.counter0 The current iteration of the loop (0-indexed)
forloop.revcounter The number of iterations from the end of the loop (1-indexed)
forloop.revcounter0 The number of iterations from the end of the loop (0-indexed)
forloop.first True if this is the first time through the loop
forloop.last True if this is the last time through the loop
forloop.parentloop For nested loops, this is the loop surrounding the current one
For more information on built-in templates tags and filters : https://docs.djangoproject.com/en/2.2/ref/templates/builtins/
Using the Django for loop It am generating a paginated list from the database. This works. The generated list has a delete button to the right of each entry.
At the same page load I an using the the django for loop to generate individual Modal windows that match each delete button. So the button will call modal ID=### and there is a modal ### that matches. The HTML appears to render perfectly.
When I delete the TOP(First entry on the page) it works like a charm, I can do that all day long, with entries moving up and being deleted.
THE PROBLEM: When I choose a 2nd position or lower button the screen goes grey and freezes it needs a reload to respond again. This pattern is repeatable.
HTML:
{% load static %}
{% block content %}
<link rel="stylesheet" href="{% static "css/cabinet_table.css"%}">
<div class="col-lg-2">
</div>
<div class="col-lg-8">
<div class="table-responsive">
<table class="table table-hover">
<th colspan="3"style="text-align: center;"><h2>{{ user.username|capfirst }} Notes</h2></th>
{% for note in note_list %}
<tr>
<td >{{ note.title }}</td>
<td>{{ note.text|truncatewords:15 }}</td>
<td><button type="button" class="btn btn-info btn-success" data-toggle="modal" data-target="#deleteModal{{ note.id }}">Delete</button></td>
</tr>
{% endfor %}
</table>
</div>
<!-- Pagination below -->
{% if note_list.has_other_pages %}
<ul class="pagination">
{% if note_list.has_previous %}
<li>«</li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in note_list.paginator.page_range %}
{% if note_list.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li>{{ i }}</li>
{% endif %}
{% endfor %}
{% if note_list.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
</div>
<div class="col-lg-2">
</div>
{% include 'cabinet/_note_list_modal.html' %}
{% endblock %}
Included HTML(The modal generation):
{% for note in note_list %}
<!-- Modal {{ note.id }} -->
<div class="modal fade" id="deleteModal{{ note.id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel{{ note.id }}">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel{{ note.id }}">Delete Note</h4>
</div>
<div class="modal-body">
<h4>Title :</h4> {{ note.title }}<br>
<h4>Idea:</h4> {{ note.text|truncatewords:25 }}
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-danger" onclick="settleUp{{ note.id }}()" >Delete</button>
</div>
</div>
</div>
</div>
<div id="hiddenForm{{ note.id }}" style="display: none" class="visibility=hidden">
<form class="" action="/fc/delete/{{ note.id }}" name="hiddenForm{{ note.id }}" method="post">
{% csrf_token %}
<input type="hidden" name="deleteNote{{ note.id }}" id="deleteNote{{ note.id }}" value="{{ note.id }}">
<!-- <button type="submit" name="betBalance">Update Balance</button> -->
</form>
<script type="text/javascript">
function settleUp{{ note.id }}(){
document.forms.hiddenForm{{ note.id }}.submit()
}
</script>
{% endfor %}
OBJECTIVE: Click on any delete button have its modal pop up and work.
Thanks for any help.
PS using inspect, which I don't know how to use well, I see no errors in the console.
The problem with this, which was solved in another question, is there is a missing DIV tag. Thanks for everyone who looked.
I am having problem in for loop. I want to break the loop suddenly after the if condition is true for the first time ie. i want to print hello only once for one 'for loop' iteration But i come to know that there is no break statement in django. So please tell me how to implement it. I think it can be done through filter but don't know how. Please tell me the approach to do this using filter or other better approach if any.
{% for p in subbranch_list %}
{% if q.id == p.parentbranch_id %}
<h1>Hello</h1>
{{ break }}
{% endif %}
{% endfor %}
Actually what i want to do is in my project.html page-
In above image there is a sidebar contain some branches of project(IT,CS), which is again categorize into subbranches (IT1, IT2 TE) and there is a small down errow in IT, which i need to display only if atleast one subbranch of it exists in subbranch table.
My project.html file is-
<div id="wrapper" class="midDiv_project">
<!-- Navigation -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="row" style="background-color:#AEAEAE;">
<div class="col-md-3" style="background-color:hsl(0, 0%, 97%);min-height:580px;margin-lef:-100px;width:21%;border-right: 1px solid #e7e7e7;">
<div class="navbar-default sidebar" role="navigation" >
<div class="sidebar-nav navbar-collapse">
<ul class="nav" id="side-menu">
<li>
<i class="fa fa-dashboard fa-fw"></i> Dashboard
</li>
<!-- Sidebar -->
{% if branch_list %}
{% for q in branch_list %}
<li>
<a href="#" class="slidebar_a"><i class="fa fa-table fa-fw"></i> {{ q.branch_title }}
{# hello #}
{% for p in subbranch_list %}
{% if q.id == p.parentbranch_id %}
<span class="fa arrow"></span></a>
{{ break }}
{% endif %}
{% endfor %}
{% for p1 in subbranch_list %}
<ul class="nav nav-second-level">
{% if q.id == p1.parentbranch_id %}
<li>{{ p1.subbranch_title }}</li>
{% endif %}
</ul>
{% endfor %}
</li>
{% endfor %}
{% endif %}
</ul>
</div>
<!-- /.sidebar-collapse -->
</div>
<!-- /.navbar-static-side -->
</div>
<div class="col-md-9" >
<div>
<div class="container" >
<div class="row">
<h2>Projects List</h2>
{% if project_list %}
{% for q in project_list %}
<div class="col-md-3" style="border:1px solid blak;height:250px;margin:2%">
<a data-toggle="modal" href="#{{ q.project_title }}">
<img class="img-responsive img-hover" src="../../../media/{{q.project_image}}" alt="Error" style="height:70%; width:100%" /></a><br>
<center><p>{{ q.project_title }}</p></center><br>
</div>
{% endfor %}
{% else %}
<p>No Project Found</p>
{% endif %}
My model.py file is-
class branch(models.Model):
branch_title = models.CharField(max_length=50)
class subbranch(models.Model):
parentbranch = models.ForeignKey(branch)
subbranch_title = models.CharField(max_length=50)
class project(models.Model):
project_title = models.CharField(max_length=50)
project_image = models.ImageField(upload_to="Images/Project")
project_desc = models.TextField(max_length=5000)
project_duration = models.CharField(max_length=50)
project_branch = models.ForeignKey(branch)
project_subbranch = models.ForeignKey(subbranch)
there is a line-
{% if q.id == p.parentbranch_id %}
and i need that after the if condition is true for first time the loop must terminate.
Probably best is to do this in your view like brandon suggested. But if you want to do it in your template here is an example of how break and continue are implemented as template filters.