I need to perform mathematical operations on the variable product.has_default_option.
how can I do?
this is my snippet:
{% for product in products %}
<li class="{{ product.css_class }}">
<a href="{{ product.url }}">
<img alt="Image of {{ product.name | escape }}" src="{{ product.image | product_image_url | constrain: '900' }}">
<b style="font-family: 'Bevan', cursive;text-transform: uppercase;font-size: large;">{{ product.name }}</b>
<i style="font-family: 'Bevan', cursive;text-transform: uppercase;font-size: medium;">{{ product.default_price | money: theme.money_format }}</i>
<p> {{ product.default_price * 2 | money: theme.money_format }}</p>
{% case product.status %}
{% when 'active' %}
{% if product.on_sale %}<em>On Sale</em>{% endif %}
{% when 'sold-out' %}
<em>Sold Out</em>
{% when 'coming-soon' %}
<em>Coming Soon</em>
{% endcase %}
</a>
</li>
{% endfor %}
You can use the times filter, so to multiply that variable by 2 you'd do the following:
{{ product.default_price | times: 2 }}
Related
What is the best practice to pass data from routes to template so that it doesn't break the template?
I have a template that uses bootstrap grid that is N rows by 3 columns.
Currently I am calculating the number of rows required in routes, and passing a list to the template to iterate through using Jinja2. I am doing this because I can't seem to import Math in the template, or perform some of the basic python operations like len(mylist)
As a result of this "hack", I have to identify the elements using 3*row + i. It works, but this hard-coding is triggering my OCD!
Can any more seasoned developers share what you would do for such cases?
#app.route('grid')
def grid():
items = Items.query.all()
items_row = list(range(0, ceil(len(items)/3) )) # Because I cannot round-up on Jinja2
...
return render_template('grid.html', items=items, item_rows=item_rows)
{% for row in item_rows %}
{% if 3*row + 0 < items|length %}
<div class="row">
<div class="col-lg-4">
<a href="{{ url_for('item', id=items[3*row + 0].id) }}"
<h1>{{ items[3*row + 0].header }}</h1>
</a>
<a href="{{ url_for('item', id=items[3*row + 0].id) }}"
<p>{{ items[3*row + 0].body }}</p>
</a>
</div>
</div>
{% endif %}
{% if 3*row + 1 < items|length %}
<div class="row">
<div class="col-lg-4">
<a href="{{ url_for('item', id=items[3*row + 1].id) }}"
<h1>{{ items[3*row + 1].header }}</h1>
</a>
<a href="{{ url_for('item', id=items[3*row + 1].id) }}"
<p>{{ items[3*row + 1].body }}</p>
</a>
</div>
</div>
{% endif %}
{% if 3*row + 2 < items|length %}
<div class="row">
<div class="col-lg-4">
<a href="{{ url_for('item', id=items[3*row + 2].id) }}"
<h1>{{ items[3*row + 2].header }}</h1>
</a>
<a href="{{ url_for('item', id=items[3*row + 2].id) }}"
<p>{{ items[3*row + 2].body }}</p>
</a>
</div>
</div>
{% endif %}
I've refactored my code:
It is much more concise now
It is also much harder to read / understand
{% set rows = 3 %}
{% set cols = 3 %}
{% for item in items %}
{% if loop.index0 // rows != (loop.index0 - 1) // rows %}
<div class="row">
{% endif %}
<div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
<a href="{{ url_for('item.html', id=item.id) }}">
<h1>{{ item.header }}<h1>
</a>
<p>{{ item.body }}</p>
</div>
{% if loop.index0 % cols == 2 %}
</div>
{% endif %}
{% endfor %}
On the category page, the sub-category are getting repeated in refine search. I have added categories and sub-categories. I have 3 level categories i.e Phone-cases(Parent)->Apple(Sub-category)->iPhone X(Sub-category of apple).
Screenshot:
<?php
{{ header }}
<div id="product-category" class="container">
<ul class="breadcrumb">
{% for breadcrumb in breadcrumbs %}
<li>{{ breadcrumb.text }}</li>
{% endfor %}
</ul>
<div class="row">
{{ column_left }}
{% if column_left and column_right %}
{% set class = 'col-sm-6' %}
{% elseif column_left or column_right %}
{% set class = 'col-sm-9' %}
{% else %}
{% set class = 'col-sm-12' %}
{% endif %}
<div id="content" class="{{ class }}">
{{ content_top }}
<h2>{{ heading_title }}</h2>
{% if thumb or description %}
<div class="row">
{% if thumb %}<div class="col-sm-2"><img src="{{ thumb }}" alt="{{ heading_title }}" title="{{ heading_title }}" class="img-thumbnail" /></div>
{% endif %}
{% if description %}<div class="col-sm-10">{{ description }}</div>
{% endif %}</div>
<hr>
{% endif %}
{% if categories %}
<h3>{{ text_refine }}</h3>
{% if categories|length <= 5 %}
<div class="row">
<div class="col-sm-3">
<ul>
{% for category in categories %}
<li>{{ category.name }}</li>
{% endfor %}
</ul>
</div>
</div>
{% else %}
<div class="row">
{% for category in categories|batch((categories|length / 4)|round(1, 'ceil')) %}
<div class="col-sm-3">
<ul>
{% for child in category %}
<li>{{ child.name }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
</div>
<br />
{% endif %}
{% endif %}
{% if products %}
<div class="row">
<div class="col-md-2 col-sm-6 hidden-xs">
<div class="btn-group btn-group-sm">
<button type="button" id="list-view" class="btn btn-default" data-toggle="tooltip" title="{{ button_list }}"><i class="fa fa-th-list"></i></button>
<button type="button" id="grid-view" class="btn btn-default" data-toggle="tooltip" title="{{ button_grid }}"><i class="fa fa-th"></i></button>
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="form-group">{{ text_compare }}
</div>
</div>
<div class="col-md-4 col-xs-6">
<div class="form-group input-group input-group-sm">
<label class="input-group-addon" for="input-sort">{{ text_sort }}</label>
<select id="input-sort" class="form-control" onchange="location = this.value;">
{% for sorts in sorts %}
{% if sorts.value == '%s-%s'|format(sort, order) %}
<option value="{{ sorts.href }}" selected="selected">{{ sorts.text }}</option>
{% else %}
<option value="{{ sorts.href }}">{{ sorts.text }}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
<div class="col-md-3 col-xs-6">
<div class="form-group input-group input-group-sm">
<label class="input-group-addon" for="input-limit">{{ text_limit }}</label>
<select id="input-limit" class="form-control" onchange="location = this.value;">
{% for limits in limits %}
{% if limits.value == limit %}
<option value="{{ limits.href }}" selected="selected">{{ limits.text }}</option>
{% else %}
<option value="{{ limits.href }}">{{ limits.text }}</option>
{% endif %}
{% endfor %}
</select>
</div>
</div>
</div>
<div class="row">
{% for product in products %}
<div class="product-layout product-list col-xs-12">
<div class="product-thumb">
<div class="image"><img src="{{ product.thumb }}" alt="{{ product.name }}" title="{{ product.name }}" class="img-responsive" /></div>
<div>
<div class="caption">
<h4>{{ product.name }}</h4>
<p>{{ product.description }}</p>
{% if product.price %}
<p class="price">
{% if not product.special %}
{{ product.price }}
{% else %}
<span class="price-new">{{ product.special }}</span>
<span class="price-old">{{ product.price }}</span>
{% endif %}
{% if product.tax %}
<span class="price-tax">{{ text_tax }} {{ product.tax }}</span>
{% endif %}
</p>
{% endif %}
{% if product.rating %}
<div class="rating">
{% for i in 1..5 %}
{% if product.rating < i %}
<span class="fa fa-stack"><i class="fa fa-star-o fa-stack-2x"></i></span>
{% else %} <span class="fa fa-stack"><i class="fa fa-star fa-stack-2x"></i><i class="fa fa-star-o fa-stack-2x"></i></span>
{% endif %}
{% endfor %}
</div>
{% endif %}
</div>
<div class="button-group">
<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>
<button type="button" data-toggle="tooltip" title="{{ button_wishlist }}" onclick="wishlist.add('{{ product.product_id }}');"><i class="fa fa-heart"></i></button>
<button type="button" data-toggle="tooltip" title="{{ button_compare }}" onclick="compare.add('{{ product.product_id }}');"><i class="fa fa-exchange"></i></button>
</div>
</div>
</div>
</div>
{% endfor %} </div>
<div class="row">
<div class="col-sm-12 text-left">{{ pagination }}</div>
{# <div class="col-sm-6 text-right">{{ results }}</div> #}
</div>
{% endif %}
{% if not categories and not products %}
<p>{{ text_empty }}</p>
<div class="buttons">
<div class="pull-right">{{ button_continue }}</div>
</div>
{% endif %}
{{ content_bottom }}</div>
{{ column_right }}</div>
</div>
{{ footer }}
Okay, this looks the relevant piece.
{% if categories %}
<h3>{{ text_refine }}</h3>
{% if categories|length <= 5 %}
<div class="row">
<div class="col-sm-3">
<ul>
{% for category in categories %}
<li>{{ category.name }}</li>
{% endfor %}
</ul>
</div>
</div>
{% else %}
<div class="row">
{% for category in categories|batch((categories|length / 4)|round(1, 'ceil')) %}
<div class="col-sm-3">
<ul>
{% for child in category %}
<li>{{ child.name }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}</div>
<br />
{% endif %}
{% endif %}
You have the else triggering I think, as you have more than 5 categories.
As you can see, the child in that category gets printed instead of the category that you're trying to trigger. Rather remove the if/else and loop straight through the categories. Each of your children falls under multiple categories as well, therefore duplication.
{% if categories %}
<h3>{{ text_refine }}</h3>
<div class="row">
<div class="col-sm-3">
<ul>
{% for category in categories %}
<li>{{ category.name }}</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
Is probably what you want, or if you only want the first category with its children:
{% if categories %}
<h3>{{ text_refine }}</h3>
<div class="row">
<div class="col-sm-3">
<ul>
{% for child in category %}
<li>{{ child.name }}</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
I use a model for the posts of my blog that you can see here(Is a my old post).
As you can see, in that moldel, I've an option to indicate highlighted post. If I use the code below to implement the pagination on my blog, also the highlighted post is sent in a page different from the first.
{% for post in posts %}
{% if post.highlighted == 1 %}
<h1><strong>Highlighted</strong></h1>
<img src="{{ post.header_image }}" alt="Image of {{ post.title }}">
<h1>{{ post.title }}</h1>
<h4>{{ post.tagline }}</h4>
{% endif %}
{% endfor %}
<hr><hr><hr>
{% for post in posts %}
{% if post.highlighted == 0 %}
<h1><strong>Not Highlighted</strong></h1>
<img src="{{ post.header_image }}" alt="Image of {{ post.title }}">
<h1>{{ post.title }}</h1>
<p>{{ post.tagline }}</p>
<h5>{{ post.publishing_date|date }}</h5>
{% for keyconcepts in post.keyconcepts.all %}
#{{ keyconcepts }}
{% endfor %}
<hr>
{% endif %}
{% endfor %}
{% block pagination %}
{% if is_paginated %}
<div class="pagination px-auto">
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link text-center shadow" href="{{ request.path }}?page={{ page_obj.previous_page_number }}">Pagina precedente</a>
</li>
{% endif %}
<li class="page-item disabled">
<p class="page-link text-center shadow">Pagina {{ page_obj.number }} di {{ page_obj.paginator.num_pages }}.</p>
</li>
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link text-center shadow" href="{{ request.path }}?page={{ page_obj.next_page_number }}">Pagina successiva</a>
</li>
{% endif %}
</ul>
</nav>
</div>
{% endif %}
{% endblock %}
I would like to exclude all highlighted post from the pagination. Is it possible?
Below views.py
class ListPost(ListView):
model = Blog
context_object_name = 'posts'
queryset = Blog.objects.filter(category="G.I.S.") #FUNDAMENTAL FILTER
template_name = "blog/list_post.html"
paginate_by = 3
class ListPost(ListView):
queryset = Blog.objects.filter(is_highlighted=True)
context_object_name = 'posts'
template_name = "blog/list_post.html"
paginate_by = 3
Note Blog seems a misnomer. BlogPost seems more accurate.
I want to display the number of objects returned from query search. I have tried
{{ p.count }} and {{ Post.count }}
Here is my post_list.html. I have read other posts that use those methods and they do not work for me. I know I am missing something.
{% extends 'posts/base.html' %}
{% block content %}
<div class="col-sm-6 col-sm-offset-3">
<h1>{{ title }}</h1>
<form method="get" action=" ">
<input type="text" name="q" placeholder="search" value="{{ request.GET.q }}"/>
<input type="submit" value=" search"/>
</form>
create
{% for p in queryset %}
<div class="row">
<div class="col-sm-12 "> <!-- i like col-sm-6 -->
<div class="thumbnail">
{% if p.image %}
<img src='{{ p.image.url }}' class="img-responsive" />
{% endif %}
<!--<img src="..." alt="...">-->
<div class="caption">
{% if p.draft %} <h3>Staff Only: Draft</h3> {% if p.publish > today %}<h3>Staff Only: Future Post</h3> {% endif %}
{% endif %}
<h3><a href='{{ p.get_absolute_url}}'>{{p.title}}</a> <small>{{p.publish | timesince }} </small> </h3>
{% if p.user.get_full_name %}<p>Author: {{ p.user.get_full_name }}</p>{% endif %}
<p>{{p.content | truncatechars:30}}</p>
<p>View
{% if user.is_authenticated %}
edit
delete
{% endif %}
</p>
</div>
</div>
</div>
<hr>
</div>
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if queryset.has_previous %}
previous
{% endif %}
<span class="current">
Page {{ queryset.number }} of {{ queryset.paginator.num_pages }}.
</span>
{% if queryset.has_next %}
next
{% endif %}
</span>
</div>
</div>
{% endblock content %}
Any help will be greatly appreciated. Thanks
You cannot do count on a single object p, because there's no count for that. Only queryset has count method, so do:
{{ queryset.count }}
You'll want to use the the built-in filter length
Returns the length of the value. This works for both strings and lists.
You can use it within your template to get the length of any object
{{ queryset|length }}
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 %}