if my table is like this
how can I output the number of events with the same name, like test should be 5 and hello should be 3.
Edit:
Here's my Jinja2 code snippet
{% for event in events %}
{% set count = 0 %}
<tr>
<td>{{ event.name }}</td>
{% for ticket in tickets %}
{% if ticket.event_name == event.name%}
{% set count = count + 1 %}
{% endif %}
{% endfor %}
<td>{{count}}<td>
<td>
But its not counting right
Never mind, I was able to solve it.
{% for event in events %}
{% set count = namespace(value=0) %}
<tr>
<td>{{ event.name }}</td>
{% for ticket in tickets %}
{% if ticket.event_name == event.name%}
{% set count.value = count.value + 1 %}
{% endif %}
{% endfor %}
<td>{{count.value}}<td>
<td>
Related
I have a long process that I managed to stream into a Jinja template, but now I would like to show not only results but also that could be viewed by the user as a meaning of progress.
This is my current code: it iterates over a huge collection of items, some of which produce results and others do not. I only want to show the items that match the search.
The rendering part:
lista_pantallas = buscar_componente_stream_generate(componente, atributo, valor)
return Response(stream_template('consultas/busqueda_comp.html',
lista_pantallas=lista_pantallas,
componente=componente, atributo=atributo, valor=valor,
error_msg=err_msg))
This is the way I generate the iterator:
def buscar_componente_stream_generate(componente, atributo, valor):
with uopy.connect(...) as session:
with uopy.File(...) as fapant:
pantallas_fmpant = uopy.List()
pantallas_fmpant.select(fapant)
functor = BuscadorObjetoAtributo(componente, atributo, valor)
for idx, pantalla in enumerate(pantallas_fmpant):
try:
if print_pant:
print(f'{idx} - Pantalla: {pantalla}')
procesar_pantalla(pantalla, functor)
for item in functor.lista_objetos():
yield item
functor.borrar_objetos()
except Exception as ex:
print('{0} - {1} - {2}'.format(idx, pantalla, str(ex)))
And the Jinja2 template
{% if lista_pantallas %}
<h1>Lista de pantallas</h1>
<h2>CondiciĆ³n: {{ componente }}.{{ atributo }} = {{ valor }}</h2>
<h2>Ultima pantalla procesada: {{idx}} - {{pantalla}}</h2>
<table>
<thead>
<th>Pantalla</th>
<th>Atributo</th>
</thead>
<tbody>
{% for item in lista_pantallas %}
{% if loop.index0 is even() %}
{% set par_css = 'par' %}
{% else %}
{% set par_css = 'impar' %}
{% endif %}
<tr class={{ par_css }}>
<td>{{ item['fichero'] }}</td>
<td>{{ item['prop'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
How can I refresh the template with the values of the variables idx and pantalla?
{% for each_item in item.artifacts %}
{% if each_item.scanner_count > 0 and each_item.scanner_match > 0 %}
{% if forloop.counter <= 5 %}
<tr>
<td>{{each_item.threat_name}}</td>
</tr>
{% else %}
{% if forloop.last %}
<p><b><i> {{ forloop.counter|add:"-5" }} rows were truncated. See full report for more details. </i></b></p>
{% endif %}
{% endif %}
{% else forloop.counter -=1 %}
{% endif %}
{% endfor %}
ERROR:Malformed template tag at line 171: "else forloop.counter -=1"
I want to increment the counter only when if condition is successful. Dont know how to do it with forloop.counter. Goal is to print 5 rows of valid output(scanner count >0 and Scanner match >0)
You can use combination of add and forloop counter to achieve what you want to achieve. But remember you need to pass some variable (I've used rank here for kinda storing the increment variable in template.
views.py
rank = 0
return render(request, "base.html", {"items": items, "rank": rank})
html
{% for each_item in items %}
{% if each_item.scanner_count > 0 and each_item.scanner_match > 0 %}
{% if forloop.counter|add:rank <= 5 %}
<tr><td>{{each_item.threat_name}}</td></tr>
<br>
{% else %}
{% if forloop.last %}
<p><b><i> {{ forloop.counter|add:"-5" }} rows were truncated. See full report for more details. </i></b></p>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
As other commenters have noted, you cannot make an assignment of the forloop.counter in the template, as that would be considered logic that should be in the view (controller). Remove the {% else forloop.counter -= 1 %} from your code and it should work as I think you intended. If not, either add logic in the object (context) being passed to the template or user other forloop attributes/variables as found in the Django documentation here: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#for
i have 2 tables in DB wallet(id,name) and balance(id,wallet_id)
i need table consisting of 2 cells (post,date)
where will all wallets in first cell and them balance in second
{% for wallets1 in wallets %}
<tr>
{% for balance1 in balance %}
{% if balance1.wallet_id == wallets1.id %}
<td> {{ balance1.balance }}</td>
{% endif %} {% endfor %}
{% endfor %}
if we have balance of coin we print balance
if balance1.wallet_id need print "0"
The difficulty next. If i do that
{% for wallets1 in wallets %}
<tr>
{% for balance1 in balance %}
{% if balance1.wallet_id == wallets1.id %}
<td> {{ balance1.balance }}</td>
{% else %}
<td> 0</td>
{% endif %} {% endfor %}
{% endfor %}
zero will be printed many times
views
wallets = Wallet.objects.all()
balance = User_balance.objects.filter(user_id= user.id)
args['wallets'] = wallets
args['balance'] = balance
return render_to_response("coins.html", args, user.id)
model
class Wallet(models.Model):
name = models.CharField(max_length=100)
class User_balance(models.Model):
user_id = models.IntegerField()
wallet_id = models.IntegerField()
balance = models.CharField(max_length=100)
You didn't post your view nor models so we have to assume a couple things but basically you're doing it wrong. Since balances have a foreign key on wallets, you don't have to loop over all balances for each wallets, you can just use the reverse relationship:
{% for wallet in wallets %}
<tr>
{% for balance in wallet.balance_set.all %}
<td> {{ balance.balance }}</td>
{% else %}
<td> 0</td>
{% endfor %}
</tr>
{% endfor %}
you must annotate balance in your view like this
wallers = Wallet.objects.all().annotate(total_balance=models.Count('balance'))
...
return render(request, 'template.html', {"wallets": wallets})
then in your html print wallets like this
<table>
<tr><th>wallet</th><th>balance</th></tr>
{% for wallet in wallets %}
<tr>
<td>{{ wallet.name }}</td>
<td>{{ wallet.total_balance|default:0 }}</td>
</tr>
{% endfor %}
</table>
Here is my code. I want that after the ending of the for loop the count should remember its last value so that i can check whether it entered in the if clause under for or not. But here it is not remembering the last value and all the time print "no coupons available" along with the coupons even if it find coupons.
So what is the solution to do so??
{% set count = 1 %}
{% for x in coupon_codes %}
{% if x[2]=="example.com" %}
<tr>
<td><code>{{ x[0] }}</code></td>
<td>{{ x[1] }}</td>
{% set count = count + 1 %}
</tr>
{% endif %}
{% endfor %}
{% if count==1 %}
<b>{% print "No Coupons Available." %}</b>
{% endif %}
You can use the filtered for feature of Jinja2:
{% for x in coupon_codes if x[2]=="example.com" %}
<tr>
<td><code>{{ x[0] }}</code></td>
<td>{{ x[1] }}</td>
</tr>
{% else %}
<b>No Coupons Available.</b>
{% endfor %}
I have many rows in a queryset. Many of the rows share the same value in two fields.
I want to show the entire queryset in a table but I don't want to print those values which are the same multiple times.
My output would be something like
mood, age, id
=============
glad, 31, 1
, , 2
, , 3
, 32, 4
, , 5
sad, 31, 6
, , 7
, 34, 8
, , 9
, , 10
happy, 40, 11
I hope it makes sense. When multiple rows share the same value I don't want to print them again. I'm using the rowspan attribute to make the rows fill the number of rows which share the same values.
I know how to do with only one field:
{% regroup my_queryset by mood as mood_list %}
{% for mood in mood_list %}
{% for node in mood.list %}
<tr>
{% if forloop.first %}
<td rowspan="{{ mood.list|length }}">{{ mood.grouper }}</td>
{% endif %}
<td>{{ node.id }}</td>
</tr>
{% endfor %}
{% endfor %}
but I have no idea how to accomplish the same when both mood and age should 'fill multiple rows'. I guess there must be some good way as I've seen other sites doing the same with many fields (e.g. in business intelligence tools).
Using the example from here and the parentloop hint from here, I have created the following code:
{% if my_queryset %}
{% regroup my_queryset by mood as data_by_mood %}
<table border="1">
<thead>
<caption>Data</caption>
<tr><th>mood</th><th>age</th><th>id</th></tr>
</thead>
<tbody>
{% for data0 in data_by_mood %}
{% regroup data0.list by age as data0_by_age %}
{% for data1 in data0_by_age %}
{% for data in data1.list %}
<tr>
{% if forloop.first %}
{% if forloop.parentloop.first %}
<td rowspan="{{ data0.list|length }}">{{ data0.grouper }}</td>
{% endif %}
<td rowspan="{{ data1.list|length }}">{{ data1.grouper }}</td>
{% endif %}
<td>{{ data.id }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
</tbody>
</table>
{% endif %}
I hope it helps (it worked for me for a similar problem).