how to loop through a condition in Django template - django

I'm attempting to display "you don't have active investment" just when there are no investments, however I've discovered that whenever the loop runs, it displays the same message multiple times when there is no investment.
I believe I have given enough information to anyone who is willing to give it a trial.
The code snippet is below, along with a screenshot at the bottom.
<h1 class="md:ml-[11.5em] mt-7 mb-0 font-bold"> Active Plan</h1>
{% for investment in investments %}
{% if investment.is_active == True %}
<div class="row justify-center my-5 md:my-3">
<div class="col-md-9 border-2 border-gray-300 rounded-lg">
<div class="pricing-table-item bg-athens-gray px-4 md:py-0">
<div class="space-y-10 md:flex justify-between items-end">
<div>
<h1 class="font-bold capitalize" style="font-family: Open Sans;">{{investment.plan}}</h1>
<div class="text-sm my-2" style="font-family: Open Sans;"><span class="text-gray-400">Invested Amount -</span> <span class="font-bold">{{investment.deposit_amount}} USD</span></div>
</div>
<div class="flex justify-between align-items-center">
<div>
<h1 class="font-bold capitalize" style="font-family: Open Sans;">{{investment.created_at}}</h1>
<div class="text-sm my-2 text-gray-400" style="font-family: Open Sans;">Start Date</div>
</div>
<div class="ml-3 mr-3">
<i class="fa-solid fa-arrow-right-long text-gray-400"></i>
</div>
<div>
{% if investment.plan == "Basic - Daily 2% for 180 Days" %}
<h1 class="font-bold capitalize" style="font-family: Open Sans;">{{investment.due_date}}</h1>
{% else %}
<h1 class="font-bold capitalize" style="font-family: Open Sans;">{{investment.due_date}}</h1>
{% endif %}
<div class="text-sm my-2 text-gray-400" style="font-family: Open Sans;">End Date</div>
</div>
</div>
<div class="md:flex justify-center items-center">
<div>
{% if investment.plan == "Basic - Daily 2% for 180 Days" %}
<h1 class="font-bold" style="font-family: Open Sans;">${{investment.investment_return}}</h1>
{% else %}
<h1 class="font-bold" style="font-family: Open Sans;">$ {{investment.investment_return}}</h1>
{% endif %}
<div class="text-sm my-2 text-gray-400" style="font-family: Open Sans;">Total Return</div>
</div>
</div>
<div>
{% if investment.plan == "Basic - Daily 2% for 180 Days" %}
<h1 class="font-bold" style="font-family: Open Sans;">$ {{investment.basic_interest}}</h1>
{% else %}
<h1 class="font-bold" style="font-family: Open Sans;">$ {{investment.premium_interest}}</h1>
{% endif %}
<div class="text-sm my-2 text-gray-400" style="font-family: Open Sans;">Net Profit Earn</div>
</div>
</div>
</div>
</div>
</div>
{% else %}
<p>You dont have active Investment</p>
{% endif %}
{% endfor %}
<h1 class="md:ml-[11.5em] mt-7 mb-0 font-bold"> Recently Ended</h1>
{% for investment in investments %}
{% if investment.is_active == False %}
<div class="">
<div class="row justify-center my-5 md:my-3">
<div class="col-md-9 border-2 border-gray-300 rounded-lg">
<div class="pricing-table-item bg-athens-gray px-4 md:py-0">
<div class="space-y-10 md:flex justify-between items-end">
<div>
<h1 class="font-bold capitalize" style="font-family: Open Sans;">{{investment.plan}}</h1>
<div class="text-sm my-2" style="font-family: Open Sans;"><span class="text-gray-400">Invested Amount -</span> <span class="font-bold">{{investment.deposit_amount}} USD</span></div>
</div>
<div class="flex justify-between align-items-center">
<div>
<h1 class="font-bold capitalize" style="font-family: Open Sans;">{{investment.created_at}}</h1>
<div class="text-sm my-2 text-gray-400" style="font-family: Open Sans;">Start Date</div>
</div>
<div class="ml-3 mr-3">
<i class="fa-solid fa-arrow-right-long text-gray-400"></i>
</div>
<div>
{% if investment.plan == "Basic - Daily 2% for 180 Days" %}
<h1 class="font-bold capitalize" style="font-family: Open Sans;">{{investment.due_date}}</h1>
{% else %}
<h1 class="font-bold capitalize" style="font-family: Open Sans;">{{investment.due_date}}</h1>
{% endif %}
<div class="text-sm my-2 text-gray-400" style="font-family: Open Sans;">End Date</div>
</div>
</div>
<div class="md:flex justify-center items-center">
<div>
{% if investment.plan == "Basic - Daily 2% for 180 Days" %}
<h1 class="font-bold" style="font-family: Open Sans;">${{investment.investment_return}}</h1>
{% else %}
<h1 class="font-bold" style="font-family: Open Sans;">$ {{investment.investment_return}}</h1>
{% endif %}
<div class="text-sm my-2 text-gray-400" style="font-family: Open Sans;">Total Return</div>
</div>
</div>
<div>
{% if investment.plan == "Basic - Daily 2% for 180 Days" %}
<h1 class="font-bold" style="font-family: Open Sans;">$ {{investment.basic_interest}}</h1>
{% else %}
<h1 class="font-bold" style="font-family: Open Sans;">$ {{investment.premium_interest}}</h1>
{% endif %}
<div class="text-sm my-2 text-gray-400" style="font-family: Open Sans;">Net Profit Earn</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% else %}
<p>You dont have active Investment</p>
{% endif %}
{% endfor %}

Something like this ?
in your views.py:
[...]
atleast_one_active=False
atleast_one_inactive=False
for investment in investments:
if investment.is_active:
atleast_one_active=True
else:
atleast_one_inactive=True
context[
...
"atleast_one_inactive":atleast_one_inactive,
"atleast_one_active":atleast_one_active,
]
return render(...)
In your HTML:
<h1 class="md:ml-[11.5em] mt-7 mb-0 font-bold"> Active Plan</h1>
{% if atleast_one_active %}
{% for investment in investments %}
{% if investment.is_active == True %}
<div class="row justify-center my-5 md:my-3">
[ ... ]
</div>
{% endif %}
{% endfor %}
{% else %}
<p>You dont have active Investment</p>
{% endif %}
<h1 class="md:ml-[11.5em] mt-7 mb-0 font-bold"> Recently Ended</h1>
{% if atleast_one_inactive %}
{% for investment in investments %}
{% if investment.is_active == False %}
<div class="">
[ ... ]
</div>
{% endif %}
{% endfor %}
{% else %}
<p>You dont have active Investment</p>
{% endif %}

Related

Invalid block tag on line 29: 'endfor', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Something wrong with my .html template. Can't figure the error on line 29. Says Invalid block tag on line 29: 'endfor', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?
{%extends 'mtunes/basic.html'%}
{% block title%}Search{% endblock %}
{% block body %}
{% load static %}
{% if query %}
<h1 style="color: rgb(158, 60, 60); text-align: center;">Search Results for {{ query_str }}</h1>
{% for i in query %}
<div class="container">
<div class="card mb-3" style="max-width: 940px; padding-top: 0%;">
<div class="row no-gutters">
<div class="col-md-4">
<img src="/media/{{i.image}}" class="card-img" alt="...">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title" style="color: green; font-weight: 550;">{{i.name}}</h5>
<p class="card-text">Singer Name: {{i.singer}}</p>
<p class="card-text">Tags: {{i.tags}}</p>
<p class="card-text">Movie: {{i.movie}}</p>
{% if user.is_authenticated %}
<button class="btn btn-outline-danger">Listen Song</button>
</div>
</div>
</div>
</div>
{% endfor %}
{% elif notfound %}
<div class="row pt-3">
<!-- you can do a lot more here -->
<h1> This song is not Available</h1>
<span style='font-size:100px;'>😂</span>
</div>
</div>
</div>
{% endif %}
{% endblock %}
You haven't ended your {% if user.is_authenticated %} tag with an {% endif %}
you didn't pass the tag in a right way and there is no such tag name {% elif notfound %} use {% else %} there and close your userauthenciation tag with with {% endif %}
{% load static %}
{% if query %}
<h1 style="color: rgb(158, 60, 60); text-align: center;">Search Results for {{ query_str }}</h1>
{% for i in query %}
<div class="container">
<div class="card mb-3" style="max-width: 940px; padding-top: 0%;">
<div class="row no-gutters">
<div class="col-md-4">
<img src="/media/{{i.image}}" class="card-img" alt="...">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title" style="color: green; font-weight: 550;">{{i.name}}</h5>
<p class="card-text">Singer Name: {{i.singer}}</p>
<p class="card-text">Tags: {{i.tags}}</p>
<p class="card-text">Movie: {{i.movie}}</p>
{% if user.is_authenticated %}
<button class="btn btn-outline-danger">Listen Song</button>
{% endif %} **you have to close user authentication tag**
</div>
</div>
</div>
</div>
{% endfor %}
{% else %} **use else here**
<div class="row pt-3">
<!-- you can do a lot more here -->
<h1> This song is not Available</h1>
<span style='font-size:100px;'>😂</span>
</div>
</div>
</div>
{% endif %}
{% endblock %}

is there is a solution for urls issues

I am building a web app using django and I get an error, I couldn't figure out what is the problem behind it.
I have three models( Category, SubCategory, Article) in my url I want something like that (localhost:8000/categories/Id_category/Id_subcategory)
the url (localhost:8000/categories/Id_category) worked perfectly but (localhost:8000/categories/Id_category/Id_subcategory) didn't work I get this error enter image description here
So here is my code that I tried to make it:
#views.py
def categorie(request):
Catégorie_list=Catégorie.objects.all()
context = {
'Catégorie_list':Catégorie_list,
}
return render(request,'articles/Category.html',context)
def souscategorie(request,categoryID):
SousCatégorie_list=SousCatégorie.objects.order_by('désignation').filter(Catégorie_identifiant_id=categoryID)
name_category=Catégorie.objects.get(id=categoryID)
articles=Article.objects.select_related('Sous_Catégorie__Catégorie_identifiant').filter(Sous_Catégorie__Catégorie_identifiant_id=categoryID)
context = {
'SousCatégorie_list':SousCatégorie_list,
'name_category': name_category,
'articles':articles
}
return render(request,'articles/SubCategory.html',context)
def articl(request,categoryID,articleID):
return render(request,'articles/article.html')
#articles/urls.py
urlpatterns=[
path('',views.categorie, name='Category'),
path('<int:categoryID>/',views.souscategorie, name='SubCategory'),
path('<int:categoryID>/<int:articleID>/',views.articl, name='article'),
path('search',views.search, name='search'),
]
#my template Category.html
{% if Catégorie_list %}
{% for category in Catégorie_list %}
<div class="col-md-6 col-lg-4 mb-4">
<div class="card listing-preview">
<a href="{% url 'SubCategory' category.id %}">
<img class="card-img-top" src="{{ category.photo_main.url }}" alt="{{ category.désignation }}"> </a>
<div class="card-body">
<div class="listing-heading text-center">
<a href="{% url 'SubCategory' category.id %}" style="text-decoration: none;">
<h4 class="text-primary" >{{ category.désignation }}</h4>
</a>
</div>
<hr>
<div class="listing-heading text-center">
<a class="text-primary" >{{ category.Description }}</a>
</div>
<hr>
Voir la catégorie
</div>
</div>
</div>
{% endfor %}
{% else %}
<div class="col-md-12">
<p>Aucune catégorie disponible</p>
</div>
{% endif %}
#my template Subcategory.html
<div class="row">
{% if articles %}
{% for article in articles %}
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="{{ article.photo_main.url }}" alt="">
<div class="card-body">
<h4 class="card-title">
{{ article.désignation }}
</h4>
<h5>{{ article.Sous_Catégorie }}</h5>
<p class="card-text">{{ article.Description }}</p>
</div>
<div class="card-footer">
<h6 class="text-muted">Ajouter au panier</h6>
</div>
</div>
</div>
{% endfor %}
{% else %}
<a class="list-group-item"> Aucune sous catégorie disponible</a>
{% endif %}
</div>

How do I make sure markdown doesn't appear when listing posts in a blog?

I'm building a blog using Django and I just integrated markdown. How do I make sure that the truncated part of the body of a post (which is telling a little of what you'll see when that particular post is opened) doesn't display markdown?
I created a template filter markdown:
from django.utils.safestring import mark_safe
from django import template
import markdown
register = template.Library()
#register.filter(name='markdown')
def markdown_format(text):
return mark_safe(markdown.markdown(text))
Here's the usage in the post_list.html template
{% extends "base.html" %}
{% load blog_tags static disqus_tags %}
{% disqus_dev %}
{% block content %}
<div class="jumbotron">
<h1 class="heading-font">Shelter At Your Crossroads</h1>
<p class="lead">...some random ass text that could serve as motto or something</p>
</div>
<div class="container">
<div class="row">
<div class="col-lg-8">
{% if tag %}
<h2 class="mb-4">Posts tagged with "{{ tag.name }}"</h2>
{% endif %}
{% for post in posts %}
<div class="card mb-5">
<img class="card-img-top img-fluid" src="{{ post.image.url }}" alt="{{ post.title }}">
<div class="card-body">
<h2 class="card-title">{{ post.title }}</h2>
<p class="card-text">{{ post.body|markdown|truncatewords_html:50 }}</p>
<i class="fa fa-book" aria-hidden="true"></i> Read More
<i class="fa fa-share-alt" aria-hidden="true"></i> Share Post
<div class="float-right">
<i class="fa fa-tags"></i>
{% for tag in post.tags.all %}
<a href="{% url 'post_list_by_tag' tag.slug %}">
<span class="badge badge-pill badge-info">{{ tag.name }}</span>
</a>
{% endfor %}
</div>
</div>
<div class="card-footer text-center text-muted">
Posted on {{ post.publish.date }} by {{ post.author.get_full_name }}
</div>
</div>
{% endfor %}
</div>
<div class="col-lg-4">
<div class="card border-dark mb-3">
<div class="card-header">Post Count</div>
<div class="card-body text-dark">
<h4 class="card-title text-center">Total Number of Posts</h4>
<p class="card-text text-center display-3">{% total_posts %}</p>
</div>
</div>
<div class="card border-dark mb-3">
<div class="card-header">Latest Posts</div>
<div class="card-body text-dark">
<h4 class="card-title text-center">Most Recent Posts</h4>
<p class="card-text">{% show_latest_posts 3 %}</p>
</div>
</div>
<div class="card border-dark mb-3">
<div class="card-header">Latest Comments</div>
<div class="card-body text-dark">
<h4 class="card-title text-center">Most Recent Comments</h4>
<p class="card-text">{% disqus_recent_comments shortname 5 50 1 %}</p>
</div>
</div>
</div>
</div>
{% include 'includes/pagination.html' with page=posts %}
</div>
{% endblock %}
The filter was used on line 22 - {{ post.body|markdown|truncatewords_html:50 }}
How do I make display just normal text?

My VideoObject isn't being picked up in my Django blog app. Does Google need time to update?

I added the appropriate markup but my videos are not showing up in Google search video tab.
Here is my markup:
{% block content %}
<div itemprop="video" itemscope itemtype="http://schema.org/VideoObject">
<meta itemprop="thumbnailUrl" content="{{post.image_url}}" />
<meta itemprop="embedURL" content="{{post.video_path}}" />
<meta itemprop="uploadDate" content="{{ post.publish }}" />
<h2 id="detail-h1">
<span itemprop="name">{{ post.title }}</span>
</h2>
{% if post.video %}
<div class="embed-responsive embed-responsive-16by9">
<iframe src="{{post.video_path}}?autoplay=1&autohide=1" class="embed-responsive-item"
frameborder="0" controls="0" allowfullscreen>
</iframe>
</div>
{% else %}
{% if post.image_url %}
<img src="{{post.image_url}}" class="img-responsive">
{% else %}
<p>upload an image</p>
{% endif %}
{% endif %}
<div style="margin-top: 10px; background: white; padding: 10px 10px 10px 10px;">
<span>
<p style="float: left">
{% if post.author %}
Author: {{post.author}}
{% endif %}
</p>
<p style="float: right" id="pub">
{%if post.draft %}<span id="draft">Draft </span>{% endif %} Publicado: {{ post.publish|date }}
</p>
</span>
<p class="tags" style="clear: both">Tags:
{% for tag in post.tags.all %}
<a href="{% url 'blog:post_list_by_tag' tag.slug %}">
{{ tag.name }}
</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>
{% if user.is_authenticated %}
<p>
delete
edit
</p>
{% endif %}
<p>Share on:
<a href="https://www.facebook.com/sharer/sharer.php?u={{ request.build_absolute_uri }}"
class="btn btn-social-icon btn-facebook" target="_blank">
<span class="fa fa-facebook"></span>
</a>
<a href="https://twitter.com/home?status={{ instance.content | truncatechars:80 | urlify }}%20{{ request.build_absolute_uri }}"
class="btn btn-social-icon btn-twitter" target="_blank">
<span class="fa fa-twitter"></span>
</a>
<a href='https://plus.google.com/share?url={{ request.build_absolute_uri }}'
class="btn btn-social-icon btn-google" target="_blank">
<span class="fa fa-google"></span>
</a>
<a href="https://www.linkedin.com/shareArticle?mini=true&url={{ request.build_absolute_uri }}&title={{
instance.title }}&summary={{ share_string }}&source={{ request.build_absolute_uri }}"
class="btn btn-social-icon btn-linkedin" target="_blank">
<span class="fa fa-linkedin"></span>
</a>
</p>
</div>
<div style="margin-top: 10px; background: white; padding: 10px 10px 10px 10px;">
<p class="content-markdown"><span itemprop="description">{{post.body}}</span></p>
</div>
<div style="margin-top: 10px; background: white; padding: 10px 10px 10px 10px; margin-bottom: 10px">
<div id="disqus_thread"></div>
</div>
</div>
{% endblock content %}
{% block aside %}
<h3 class="">Similar Posts</h3>
<hr style="margin:10px">
<div class="row">
{% for post in similar_posts %}
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-12" id="similar">
<div class="thumbnail thumb">
<h6 id="date">{{ post.publish|date }}</h6>
{% if post.image %}
<img src="{{post.image.url}}" class="img-responsive post">
{% elif post.image_url %}
<img src="{{post.image_url}}" class="img-responsive post">
{% else %}
<p>upload an image</p>
{% endif %}
<div class="caption">
<a href="{{ post.get_absolute_url }}">
<h5 class="post-title" id="title">{{post.title}}</h5>
</a>
{% if user.is_authenticated %}
<p>
delete
edit
</p>
{% endif %}
</div>
</div>
</div>
{% empty %}
<p id="para">There are no similar posts yet.</p>
{% endfor %}
</div>
{% endblock aside %}
Today is Sunday May 8th 2016. I did this about 11:30 last night.
Does Google need to update its systems for it to show up? I ran it throught the Structured Data Testing Tool and It worked. I got a green check on my VideoObject. So Google is sensing it but it's not showing.
Another thing: My blog doesn't host videos. The videos are embeds from YouTube. When someone clicks the link on Google, will it send them to my site or to the source of the embed?

Angular/bootstrap collapse button collapsing all elements

I'm new to Bootstrap, Angular and Django, so I'll try my best to be as clear as possible. I have several dynamic generated div's by a django loop, and for each div a collapse button. I set the data-target of each button to a dynamically attributed id for each element (I use a django variable that is being pulled in the loop for it), but regardless of what button I press all the div's will be collapsed/uncollapsed.
{% for country in countries %}
<div class="container">
<div class="row">
<div class="country-panel col-xs-12" style="background:url({{ country.country_image}}) no-repeat; position: relative;">
<div class="row">
<div class="country-name col-xs-4 col-xs-offset-4">
<div class="row">
<div class="col-xs-12">
<p class="title">{{ country.name | upper }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="row">
<div class="description-panel col-xs-12">
<div class="row">
<p class="content">{{ country.content }}</p>
</div>
<div class="row">
{% for city in country.cities %}
{% if forloop.counter <= 3 %}
{% if forloop.counter < 3 %}
<a><div class="col-xs-4 city-block right" style="background:url({{city.image}}) no-repeat; position: relative; z-index: 0;">
{% elif forloop.counter == 3 %}
<a><div class="col-xs-4 city-block" style="background:url({{city.image}}) no-repeat; position: relative; z-index: 0;">
{% endif %}
<div class="city-layer"></div>
<span class="city-name">{{city.name | upper}}</span>
</div></a>
{% endif %}
{% endfor %}
</div>
<div class="row" uib-collapse="isCollapsed" id="{{country.name}}">
{% for city in country.cities %}
{% if forloop.counter > 3 %}
{% if forloop.counter|divisibleby:3 %}
<a><div class="col-xs-4 city-block" style="background:url({{city.image}}) no-repeat; position: relative; z-index: 0;">
{% else %}
<a><div class="col-xs-4 city-block right" style="background:url({{city.image}}) no-repeat; position: relative; z-index: 0;">
{% endif %}
<div class="city-layer"></div>
<span class="city-name">{{city.name | upper}}</span>
</div></a>
{% endif %}
{% endfor %}
</div>
<div class="row">
<div class="col-xs-12">
<p class="content">{{country.description}}</p>
<p><div class="col-xs-2 col-xs-offset-10"><a class="btn-default pink-button more-cities col-xs-12" role="button" type="button" ng-click="isCollapsed = !isCollapsed" data-target="#{{country.name}}">{% trans "MORE CITIES" %}&nbsp<span class="fa fa-plus"></span></a></div></p>
</div>
</div>
<div class="button-separator col-xs-12"></div>
</div>
</div>
</div>
</div>
{% endfor %}
Please not that I've started web development a month or 2 ago and also First time posting on stackoverflow, so please be gentle.
Turns out that I forgot to pass the ng-controller in the collapse div, for the controller that has my collapse functions. Thank you for all the help anyway!