concise if statements in django templating system - django

Here is the monster inefficient code:
{% for link in header_links %}
{% if not link.image %}
{% if not link.url %}
<li>{{ link }}</li>
{% else %}
<li>{{ link }}</li>
{% endif %}
{% else %}
{% if not link.url %}
<li><img src="{{ link }}" /></li>
{% else %}
<li><img src="{{ link.image }}" /></li>
{% endif %}
{% endif%}
{% endfor %}
As you can see, this is ridiculous. A simple tertiary statement or two would be totally fitting, except within the {% %} blocks I can't access the variables with filters and things like that.
Here is my python/django pseduo code that expresses the same thing with the efficiency I think is possible.
{% for link in header_links %}
<li>{% print "<img src='" + link.image + "' />" if link.image else print link %}</li>
{% endfor %}
As you can see, using two tertiary statements would be awesome and much more visually efficient anyway. Yet this code doesn't work.
Any suggestions would be awesome!!
Thanks,
Django Noob
IN CLOSING:
We came to the conclusion that following the MVC paradigm leads me to do the "heavy" lifting to the controller section and give the view as little thinking as possible.
The pseudo code I will end up using will be as follows:
in the view
header_links = {}
links = Link.object.all()
for link in links:
header_links['url'] = (link.name if not link.url else link.url)
header_links['name'] = (link.name if not link.image else "<img src='" + link.image +"' />")
context = Context({
"header_links": header_links
})
in the controller
{% for link in header_links %}
<li><img src="{{ link['name'] }}" /></li>
{% endfor %}

Just rewrote your if statement and I think this way looks more obvious. Is not the most clever solution but It's more readable.
{% for link in header_links %}
{% if not link.image and not link.url%}
<li>{{ link }}</li>
{% endif %}
{% if not link.image and link.url%}
<li>{{ link }}</li>
{% endif %}
{% if link.image and not link.url%}
<li><img src="{{ link }}" /></li>
{% endif %}
{% if link.image and link.url%}
<li><img src="{{ link.image }}" /></li>
{% endif %}
{% endfor %}

How about this?
{% for link in header_links %}
<li>
{% if link.url and link.image %}
<img src="{{ link.image }}" />
{% elif not link.url %}
<img src="{{ link }}" />
{% elif not link.image %}
{{ link }}
{% else %}
{{ link }}
{% endif%}
</li>
{% endfor %}

Related

How to split a menu within a wagtail model?

I'm trying to create a navigation menu, that when the number of objects within the menu is even puts an image between the two halves of the menu. Here is the code that I tried to use within my menu model and templates to create the effect.
"""Splitting the menu in half for the frontend."""
#property
def is_even(self):
if (self.objects.count() % 2) == 0:
return True
else:
return False
#property
def find_halves(self):
first_half = self.objects[0:self.objects.count()/2]
second_half = self.objects[self.objects.count()/2:self.objects.count()]
return first_half, second_half
<nav class="main-nav">
{% image navigation.logo fill-115x115 as logo %}
<ul class="nav-links">
{% if navigation.is_even %}
{% for item in navigation.menu_items.first_half.all %}
<li><a href="{{ item.link }}" {% if item.open_in_new_tab %} target="_blank" {% endif %}>{{ item.title }}</a></li>
{% endfor %}
<img src="{{ logo.url }}" alt="CCCR Logo" class="logo">
{% for item in navigation.menu_items.second_half.all %}
<li><a href="{{ item.link }}" {% if item.open_in_new_tab %} target="_blank" {% endif %}>{{ item.title }}</a></li>
{% endfor %}
{% else %}
<img src="{{ logo.url }}" alt="CCCR Logo" class="logo">
{% for item in navigation.menu_items.all %}
<li><a href="{{ item.link }}" {% if item.open_in_new_tab %} target="_blank" {% endif %}>{{ item.title }}</a></li>
{% endfor %}
{% endif %}
</ul>
</nav>
However, when I run the code it goes to the else statement even though I have six menu items in the wagtail admin.
For an item like this that is related to what users see, I would suggest using the Django template tag divisibleby: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#divisibleby
So maybe something like this:
{% if navigation.menu_items.count|divisibleby:"2" %}
You might also find the cycle template tag useful. It might let you do what you want layout-wise by alternating the placement of a class on odd and even items.

I want to separate products based on id but tempatesyntaxterror is shown

I tried this:
{% for i in object %}
{% if i.get(id=1) %}
<img src="{{object.img1.url}}" />
<p><b>Rs {{i.price}}</b></p>
{% endif %}
{% endfor %}
But, this error is shown:
TemplateSyntaxError
Could not parse the remainder: '(id=1)' from 'i.get(id=1)'
I believe you are trying to get the id of i and trying it to check with if statement. So, try
{% for i in object %}
{% if i.id == 1 %} <--- Here
<img src="{{ i.img1.url }}" /> <--- Here also i.img1.url not object.img1.url
<p><b>Rs {{i.price}}</b></p>
{% endif %}
{% endfor %}

How to use django paginator page range for displaying the first 10 numbers not all?

I want to change the blow script to show just the first 10 page numbers. Now it shows all page numbers. Could you please tell me how to change it? I tried several ways but it didn't work. Any help will be appreciated!
{% if forloop.counter|divisibleby:"3" or forloop.last %}
</div>
{% endif %}
{% endfor %}
{% if is_paginated %}
<div class="row">
<ul class="pagination pagination-md ">
{% if page_obj.has_previous %}
<li>«</li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }}</span></li>
{% else %}
<li><a href="?page={{ i }}{% if currentCategory %}&category={{ currentCategory }}
{% endif %}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
</div>
{% endif %}
</div>

Listing Tags of a Post in Django CMS and Aldryn NewsBlog

I am trying to figure out how to display tags belonging to an article created within Aldryn NewsBlog plugin. Unfortunately, I cannot find any documentation on how do it.
I was able to display categories using the following code.
<span style="margin: 0; display: block">
<h4 style="display:inline-flex">Categories:</h4>
{% for category in article.categories.all %}
{{ category.name }} {% if not forloop.last %}, {% endif %}
{% endfor %}
</span>
For tags, I am using this code:
<span style="margin: 0; padding-bottom: 0; display: block">
<h4 style="display:inline-flex">Tags:</h4>
{% for tag in article.tag %}
{{ tag.name }} {% if not forloop.last %}, {% endif %}
{% endfor %}
</span>
What am I doing wrong? Could anyone tell me how to display tags?
this is the official tags template of aldryn-newsblog, it worked for me:
{% load i18n apphooks_config_tags %}
<div class="aldryn aldryn-newsblog aldryn-newsblog-tags">
<ul class="list-unstyled">
<li{% if not newsblog_tag %} class="active"{% endif %}>
{% trans "All" %}
</li>
{% for tag in tags %}
<li{% if newsblog_tag.id == tag.id %} class="active"{% endif %}>
<a href="{% namespace_url "article-list-by-tag" tag.slug namespace=instance.app_config.namespace default='' %}">
{{ tag.name }}
<span class="badge">{{ tag.article_count }}</span>
</a>
</li>
{% endfor %}
</ul>
https://github.com/aldryn/aldryn-newsblog/blob/master/aldryn_newsblog/boilerplates/bootstrap3/templates/aldryn_newsblog/plugins/tags.html
you're right, that is what you're looking for, with article.tags.all:
{% if article.tags.exists %}
<ul style="margin-left: 0">
{% for tag in article.tags.all %}
<li class="tags">{{ tag.name }}</li>
{% if not forloop.last %}<span class="separator tags-separator">|</span> {% endif %}
{% endfor %}
</ul>
{% endif %}

how to order products by category?

On my products page, i would like to show the products ordered by category. I tried a loop like that but have an error unknown category tag. Any clue ?
{% for category in categories.active %}
<h2>{{ category.name }}</h2>
{% for product in category.products %}
{% if forloop.first %}
<ul id="products" class="{% if forloop.length == 1 %}single_product{% endif %}{% if forloop.length == 2 %}double_product{% endif %}">
{% endif %}
<li id="product_{{ product.id }}" class="product">
<a href="{{ product.url }}" title="View {{ product.name | escape }}">
<div class="product_header">
<h2>{{ product.name }}</h2>
<span class="dash"></span>
<!--<h3>{{ product.default_price | money_with_sign }}</h3>-->
{% case product.status %}
{% when 'active' %}
{% if product.on_sale %}<h5>On Sale</h5>{% endif %}
{% when 'sold-out' %}
<h5>Sold Out</h5>
{% when 'coming-soon' %}
<h5>Coming Soon</h5>
{% endcase %}
</div>
<div class="product_thumb">
<img src="{{ product.image | product_image_url | constrain: '560' }}" class="fade_in" alt="Image of {{ product.name | escape }}">
</div>
</a>
</li>
{% if forloop.last %}
</ul>
{% endif %}
{% endfor %}
{% endfor %}
Ok just managed to do what i wanted. was on the right trail but something was messing all around. i copy it here.
{% for category in categories.active %}
<h2>{{ category.name }}</h2>
{% for product in category.products %}
{% if forloop.first %}
<ul id="products">
{% endif %}
<li id="product_{{ product.id }}" class="product">
<a href="{{ product.url }}" title="View {{ product.name | escape }}">
<div class="product_header">
<h2>{{ product.name }}</h2>
<span class="dash"></span>
<!--<h3>{{ product.default_price | money_with_sign }}</h3>-->
{% case product.status %}
{% when 'active' %}
{% if product.on_sale %}<h5>On Sale</h5>{% endif %}
{% when 'sold-out' %}
<h5>Sold Out</h5>
{% when 'coming-soon' %}
<h5>Coming Soon</h5>
{% endcase %}
</div>
<div class="product_thumb">
<img src="{{ product.image | product_image_url | constrain: '560' }}" class="fade_in" alt="Image of {{ product.name | escape }}">
</div>
</a>
</li>
{% if forloop.last %}
</ul>
{% endif %}
{% endfor %}
{% endfor %}