How can I write numbers in a cell inside Bootstrap table in accounting format?
For example: one million should be shown as 1,000,000 and not 1000000 (notice commas ',' between digits).
Please note that data data is getting filled by Django app.
Example:
<tbody>
{% for row in tbl_list %}
<tr id="port_row_{{row.stock}}_{{index}}">
{% if row.stock == 'TOTAL'%}
<td> {{row.stock}}</td>
{% else %}
<td> <a target="_blank" style="color:blue;" href="https://www.google.com/finance?q=NSE:{{ row.stock }}">{{row.stock}}</a></td>
{% endif %}
<td>{{row.name}}</td>
<td>{{row.investment_amount}}</td>
<td>
{% if row.weekly_gain >= 0 %}
<div style="color:green">
+{{row.weekly_gain}}
<i class="fa fa-arrow-up"></i>
</div>
{% else %}
<div style="color:tomato">
{{row.weekly_gain}}
<i class="fa fa-arrow-down"></i>
</div>
{% endif %}
</td>
<td>{{row.percentage}}</td>
<td>{{row.percentage_of_portfolio}}</td>
</tr>
{% endfor %}
</tbody>
It depends on where the data is coming from, but you will need to use something like Javascript to do this.
See this question for more information.
You can use the javascript accounting.js library:
// Default usage:
accounting.formatMoney(12345678); // $12,345,678.00
Since the question was specifically for Django templates, I used django.contrib.humanize when rendering price.
A set of Django template filters useful for adding a “human touch” to data.
To activate these filters, add 'django.contrib.humanize' to your INSTALLED_APPS setting. Once you’ve done that, use {% load humanize %} in a template, and you’ll have access to the following filters.
Documentation
In your code, do <td>{{ row.monday_open_price|intcomma }}</td>
Do not forget to {% load humanize %}
Related
So here's my problem: I've got a bunch of instances of a class. I would like to have a sort of table of these instance objects, so that there is a maximum of six in every row. In bootstrap terms, I would like each object to be represented by a thumbnail in a "div" of class "span2".
My initial impulse was to use a nested for loop, but I am having trouble manipulating my index variable in the template, and I can't figure out how to do so outside of my template.
Here is generally what the python/django template/pseudo code is I'm trying to figure out.
queryset = Class.objects.all()
set_length = queryset.count()
num_rows = set_length/6
#because I want 6 columns in each row, each with one instance
set_as_list = list(queryset)
# have a list so I can iterate through objects by index
for i in range(table_rows):
# make a row
<div class="row">
for j in range (i*6,(i+1)*6):
#make six or less columns
<div class="span2">
<p>set_as_list[j].attribute1</p>
<p>set_as_list[j].attribute2</p>
</div>
</div> # end row
I hope this flagrant mixing of django templating language, python, and html doesn't offend anybody too badly. just trying to express the idea of what I'm trying to do. I would appreciate any help someone may be willing to offer because I've been struggling with this for days and have done quite a bit of searching for a solution both within a template and outside.
I also realise that there will be need to be a final row with the remainder of objects after the integer division.
Have no time to explain, but I've had similar problem and until i closed this browser page here is a solution
{% for sub_article in articles %}
{% if forloop.first %}<div class="row">{% endif %}
<div class="col-xs-4">
<a href="#">
{{ sub_article.name }}
</a>
</div>
{% if forloop.counter|divisibleby:3 %}</div><div class="row">{% endif %}
{% if forloop.last %}</div>{% endif %}
{% endfor %}
Since forloop.counter starts the index with 1, divisibleby 3 does not work.
So use forloop.counter0 instead.
<div class="row">
{% for product in all_products %}
{% if forloop.counter0|divisibleby:3 %}
</div><br><div class="row">
{% endif %}
<div class="col-4"></div>
{% endfor %}
I would recommend to add a custom tag as_chunk. I think it makes the code prettier and more readable.
# app/templatetags/my_tags.py
from math import ceil
from django import template
register = template.Library()
#register.filter
def as_chunks(lst, chunk_size):
limit = ceil(len(lst) / chunk_size)
for idx in range(limit):
yield lst[chunk_size * idx : chunk_size * (idx + 1)]
# app/templates/your-template.html
{% load my_tags %}
...
{% for chunk in elements|as_chunk:6 %}
<div class="row">
{% for element in chunk %}
<div class="col-2">
{{ element.name }}
</div>
{% endfor %}
</div>
{% endfor %}
...
maybe too late but there is simple solution as follow
<div class="container">
<div class="row">
{% for product in products %}
{% if forloop.counter0|divisibleby:3 and not forloop.first %}<div class="w-100"></div>{% endif %}
<div class="col">{{product.title}}</div>
{% endfor %}
</div>
</div>
You could make the code a bit more generic. Here's the logic:
queryset = Class.objects.all()
set_length = queryset.count()
<div class="row">
{% for i in queryset %}
<div class="span2">
<p>i.attr</p>
<p>i.attr</p>
</div>
{% if forloop.counter|divisibleby:"6" or forloop.last %}
</div> <!--end row-->
{% endif %}
{% endfor %}
I hope this solves your problem :-)
I have a Jekyll template pulling in text from data objects.
eg.
{% for speaker_hash in site.data.2015.speakers %}
{% assign speaker = speaker_hash[1] %}
<li>
<div class="speaker">
<img class="head" src="/img/2015/speakers/sample.jpg">
<h2> {{ speaker.name}} </h2>
</div>
</li>
{% endfor %}
However I would like to have each page specify what year its for with a page.year property.
Is it possible to create the same for loop but specify the year dynamically?
eg
{% for speaker_hash in site.data.[page.year].speakers %}
Answer yes.
1 - Your page.year must be a string as hash indexes are strings. So in you front matter : year: '2015'
2 - Get speakers depending on page.year :
{% for speaker_hash in site.data[{{page.year}}].speakers %}
I'm building an ecommerce site with django, and i wanted to create a button that would signal that an order had already been delivered.
I'm pretty sure you can acheive this with a checkbox, but i wanted to use a button, because it would be easier to click when using a tablet.
I want the button to also be "unclickable" in case someone accidentally clicked a button for a wrong order.
Do i need to make a form in the html? or is there an easier way to do it.
this is my html:
Order page
<table>
<tr>
<td>#</td>
<td>Name</td>
<td>Email</td>
<td>Phone</td>
<td>Order</td>
<td>Order Quantity</td>
<td>Delivered</td>
</tr>
{% for ord in orders %}
{% for food in ord.orderitem_set.all %}
<tr>
{% if forloop.counter == 1 %}
<td>{{ord.pk}}</td>
<td>{{ord.user.first_name}}</td>
<td>{{ord.user.email}}</td>
<td>{{ord.user.get_profile.phone}}</td>
{% else %}
<td colspan="4"></td>
{% endif %}
<td>{{food.name}}</td>
<td>{{food.quantity}}</td>
<td>{% if forloop.counter == 1 %} <button type="button">Delivered</button> {% endif %}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
</body>
</html>
hmm, I think just putting a form around the button would be the "easier way" ^^
later if you wanted to, you could make it a spiffy javascript toggle using almost the same view code below maybe
e.g. around your buttons...
<form action="{% url show_orders %}" method="post">
<input type="hidden" name="order-id" value="{{ ord.pk }}"/>
<input type="hidden" name="action=" value="toggledelivery"/>
<button type="button">{% if not ord.is_delivered %}Not {% endif %}Delivered</button>
</form>
then in your view, something like...
def show_orders(request):
if request.method == "POST":
order_id = request.POST.get('order-id', None)
# TODO toggle the order here
return HttpResponseRedirect(back_to_the_order_admin_page)
else:
# ...show the admin page
Recently at work there has been some discussion as to how best present a bit of data, and there seems to be some internal conflict as to whether or not certain pages should use tables to display the information vs. creating psuedo-tables with Bootstrap.
This is what source would look like if tables we decided to go ahead and use tables to display the information. Some of the conditional guff has been removed.
<table class="table well">
<thead>
<th> Name </th>
<th> Detail #1 </th>
<th> Detail #2 </th>
<th> Detail #3 </th>
</thead>
<tbody class="sortable">
{% for m in model_list %}
<tr id="{{ m.id }}">
<td><span class='number-style'> {{ forloop.counter }}.</td>
<td>{{ m.name}}</td>
<td>{% autoescape off %}{{ m.detail1 }}{% endautoescape %} </td>
<td>{% autoescape off %}{{ m.detail2}}{% endautoescape %} </td>
<td>{% autoescape off %}{{ m.detail3}}{% endautoescape %} </td>
</tr>
{% endfor %}
</tbody>
and this is it using bootstrap divs
{% for m in model_list %}
<div class="row">
<div class="span2">
{{ m.name }}
</div>
<div class="span2" style="word-wrap: break-word">
{% autoescape off %}{{ m.details1 }}{% endautoescape %}
</div>
<div class="span2" style="word-wrap: break-word">
{% autoescape off %}{{ m.details2 }}{% endautoescape %}
</div>
<div class="span2" style="word-wrap: break-word">
{% autoescape off %}{{ m.details3 }}{% endautoescape %}
</div>
</div>
{% endfor %}
Keeping in mind that both of these are contained within a page laid out with bootstrap, and that their may be multiple instances of each of these "tables" on the page. Which method would be more generally preferred?
Do each and every row have the same datatype for detail 1? Similarly for detail 2 and 3?
If the relation between the rows is that all have 3 details but those 3 details can be of various types and not specifically of type 1, type 2 and type 3, then using table is NOT the right way.
For eg if I am listing books and their details are fixed and of type [author, category, cost], then I would definitely use a table.
But if for some books I'm listing [author, category, cost], for some I'm listing [author, publisher, edition] and for others a different set altogether, then table is not the right way. Tables should not be used for visual styling and arrangement.
Seeing your code I get a strong feeling that details are of same data type and hence should be presented in a table.
This part is from views.py
results=[(A,[stuObj1,stuObj2,stuObj3]),(B,[stuObj4,stuObj5,stuObj6]),(C,[stuObj7,stuObj8])]
for tup in results:
total = tot+len(tup[1])
render_to_response(url,{'results':res , 'total':str(tot),})
this is template code:
<th class="name">Name</th>
<th class="id">Student ID</th>
<th class="grade">Grade</th>
{% for tup in results %}
{% for student in tup|last %}
{% with forloop.parentloop.counter as parentloopid %}
{% with forloop.counter as childloopid %}
<tbody class="results-body">
<tr>
<td>{{student.fname|lower|capfirst}} {{student.lname|lower|capfirst}}</td>
<td>{{student.id}}</td>
<td>{{tup|first}}</td>
</tr>
{% endfor %}
{% endfor %}
Now the problems am having are
numbering the rows. Here my problem is am not sure if i can do things like total=total-1 in the templates to get the
numbered rows like <td>{{total}}</td>
applying css to tr:even or odd.
Whats happening in this case is everytime the loop is running the odd/even ordering is lost.
these seems related problems. Any ideas would be great :)
For numbering the rows you can use the forloop.counter
Here you can see an example how to use it.
To switch between even and odd you can use the cycle template tag
{% for project in object_list %}
<tr class="{% cycle odd,even %}"> ... </tr>
{% endfor %}
answer for my question:
Numbering would be a bit time taking. one option is to design custom filters or other way is to modify views and use simple forloop.counter to add, count and forloop.counter. Let me give an example: for the above cases, results are sorted dictionaries with grades and students,something like this ((A:a,b,c,d,e), (B:f,g,h,i), (C:j,k,l,m,n)). In the view add one more dictionary to each tuple with the student count of previous tuple.
temp_count = 0
for tup in results:
tup[1].append({'count':temp_count})
temp_count = temp_count + len(tup[1])-1
-1 is because we don't want to avoid dictionary counted
Inside the template
{% with tup|last|last as cnt %}
{% with forloop.counter as rnum %}
{% with rnum|add:cnt.count as rownum %}
<td>{{rownum}}</td>
rest of code goes here
{% endwith %}
{%endwith%}
{% endwith %}
using {% cycle %} won't be that helpful when using nested loops,
<tr class="{% if rownum|divisibleby:2 %}"even"{% else %}"odd"{% endif %}">
follow this clean way to color.