Preventing flask from printing new line in a loop - python

I am trying to produce table headings using loop in flash but I get new lines after each:
{% extends "layout.html" %}
{% block title %}
Portfolio overview
{% endblock %}
{% block main %}
{% for variable in variables -%}
{% for key, value in variable.items() -%}
<table>
<thead><th>{{ key }}</th></thead>
{% endfor -%}
{% for key, value in variable.items() -%}
<tbody>
<td>{{ value }}</td>
</tbody>
{% endfor -%}
{%- endfor -%}
</table>
<h2>Cash available (US$): {{ cash }}</h2>
<h2>Total portfolio value (US$): {{ wealth }}</h2>
{% endblock %}
Result
However, I would like to get this
Desired result

Give this a shot;
{% extends "layout.html" %}
{% block title %}
Portfolio overview
{% endblock %}
{% block main %}
<table>
{% for variable in variables %}
<thead>
{% for key, value in variable.items() %}
<th>{{ key }}</th>
{% endfor %}
</thead>
<tbody>
{% for key, value in variable.items() %}
<td>{{ value }}</td>
{% endfor %}
</tbody>
{% endfor %}
</table>
<h2>Cash available (US$): {{ cash }}</h2>
<h2>Total portfolio value (US$): {{ wealth }}</h2>
{% endblock %}
I just fixed up the placement of your for loops.

Related

nested block and for loop in a Jinja template block

Trying to use nested block and for loop in a Jinja template block setup.
{% block main %}
<table>
<tr>
<td>user id</td>
<td>user sec level</td>
</tr>
{% block main_nested_b scoped %}
{%
for user in list_users:
t_id_user = str(user[0][0])
t_sec_level = str(user[2][0])
%}
<tr>
<td>
<a href='/usersEdit?id_user={{ t_id_user }}' class='onwhite'>edit</a>
</td>
</tr>
{% endfor %}
{% endblock main_nested_b %}
{% endblock main %}
</table>
Error message:
jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 't_id_user'
Help?
You can't treat Jinja syntax as Python syntax. It's not the same thing. Keep your for tag separate from assignment (set) tags:
{% for user in list_users %}
{% set t_id_user = user[0][0] %}
{% set t_sec_level = user[2][0] %}
Note that there isn't even a : at the end of the for ... in ... syntax! Also you don't need to call str() here, leave that to Jinja to convert to strings for you; anywhere you use {{ t_id_user }} or {{ t_sec_level }} the value will be converted to a string anyway.
Here is the complete template:
<table>
{% block main %}
{% block main_nested_b scoped %}
{% for user in list_users %}
{% set t_id_user = user[0][0] %}
{% set t_sec_level = user[2][0] %}
<tr>
<td>
<a href='/usersEdit?id_user={{ t_id_user }}' class='onwhite'>edit</a>
</td>
</tr>
{% endfor %}
{% endblock main_nested_b %}
{% endblock main %}
</table>

Django render() not passing variable to template?

def get_queryset(self):
...
#days = {day1 : [[FreetimeStart, FreetimeEnd], [[Meeting1Start, Meeting1End], ...]], ...}
days = {}
for i in range(len(compilation)):
days[daylist[i]] = compilation[i]
print(days)
return render(self.request, 'calendar.html', {"days":days})
Above (with nonrelevant code omitted) is a function from a Django view I've been writing. It's supposed to send the days dictionary over to the template calendar.html (shown below), but it's not- I've confirmed via the print(days) statement that days is being generated properly and that the function is being called. I'm not getting any errors, either.
{{ days }}
<table>
<tr>
{% for key in days %}
<th>
{{ key }}
</th>
{% endfor %}
</tr>
<tr>
{% for value in days.values %}
<td>
{% if value.0 %}
Free time: {{ value.0.0 }} to {{ value.0.1 }}
{% else %}
Free time: None
{% endif %}
</td>
{% endfor %}
</tr>
<tr>
{% for value in days.values %}
<td>
{% for meeting in value.1 %}
Meeting from {{ meeting.0 }} to {{ meeting.1 }}</br>
{% endfor %}
</td>
{% endfor %}
</tr>
</table>
What am I doing wrong here? There doesn't seem to be anything wrong with my render() arguments.

jinja2, having trouble with some loops and two table objects

This is my code:
<!-- Loop thru Table Macro begins-->
{% macro loopThruTable(currentParent, prod)%}
<li> {{prod.category_name}} </li>
{% set currentParent = prod.id %}
{{ checkParent(currentParent) }}
<ul>
<table>
<tr>
<th>Service</th>
<th>Price</th>
</tr>
{{ checkParentService(currentParent)}}
</table>
</ul>
{% endmacro %}
<!-- Loop thru Table Macro ends-->
<!-- CheckParent Macro begin-->
{% macro checkParent(currentParent) %}
{% for prod in myProd %}
{% if prod.parent_category_id == currentParent %}
<ul>
{{ loopThruTable(currentParent, prod) }}
</ul>
{% endif %}
{% endfor %}
{% endmacro %}
<!-- checkParent Macro Ends-->
<!-- CheckParentService Macro begin-->
{% macro checkParentService(currentParent) %}
{% for serv in myServ %}
{% if serv.parent_category_id == currentParent %}
<tr>
{{ loopThruTableService(currentParent, serv.name) }}
{{ loopThruTableService(currentParent, serv.price) }}
</tr>
{% endif %}
{% endfor %}
{% endmacro %}
<!-- checkParent Macro Ends-->
<!-- Loop thru Table Macro begins-->
{% macro loopThruTableService(currentParent, blah) %}
<td>{{blah}}</td>
{% endmacro %}
<!-- Loop thru Table Macro ends-->
{% extends 'admin/master.html' %}
{% block body %}
<ul>
{% set parent = 0 %}
{{ checkParent(parent) }}
</ul>
{% endblock %}
for all intents and purposes it is running mostly correctly, but it is adding an extra table at the end of each loop.
enter image description here
as you can see in the bottom, there's a "ServicePrice" that is separating each category.
enter image description here
here's another example. I know it has to do with my loops, but as far as jinja2 is concerned, I don't know if there's a method that exists where i check if the parent just 'exists' in my object.
Thanks,
I figured it out.
I had to add one extra for loop and if statement in the loopThruTable Macro.
It now looks like this:
{% macro loopThruTable(currentParent, prod)%}
<li> {{prod.category_name}} </li>
{% set currentParent = prod.id %}
{{ checkParent(currentParent) }}
{% for serv in myServ %}
{% if serv.parent_category_id == currentParent %}
<table>
<tr>
<th>Service</th>
<th>Price</th>
</tr>
{{ checkParentService(currentParent)}}
</table>
{% endif %}
{% endfor %}
{% endmacro %}
I'm still not entirely sure why that works. But it works.

Crispy-Forms Include Tag Causing Many Duplicate Templates

I have a Django site that uses the below template to render a crispy-forms model formset. Using django-debug-toolbar, I gathered that the include tags are rendering the bootstrap4 templates many, many times. I think this is what is killing my performance (i.e. 3-4 minutes to load an inline formset with 100 forms in it)
How should I replace the include tags to avoid the duplicate rendering? Should I use extend somehow?
I can replace the include tags with the actual html from the bootstrap4 crispy-forms templates, but those templates have nested templates as well. That creates an exercising of building a master crispy-forms template that includes everything...which seems like the wrong way to go about this. In addition, I tried replace the bootstrap4/field.html include tag with the actual html, and field.html was still duplicated, and the data lost it's table structure because of the loss of with tag='td'.
{% load crispy_forms_tags %}
{% load crispy_forms_utils %}
{% load crispy_forms_field %}
{% specialspaceless %}
{% if formset_tag %}
<form {{ flat_attrs|safe }} method="{{ form_method }}" {% if formset.is_multipart %} enctype="multipart/form-data"{% endif %}>
{% endif %}
{% if formset_method|lower == 'post' and not disable_csrf %}
{% csrf_token %}
{% endif %}
<div>
{{ formset.management_form|crispy }}
</div>
<div class='table-responsive'>
<table{% if form_id %} id="{{ form_id }}_table"{% endif%} class="table table-hover table-sm" id='dispositionTable'>
<thead>
{% if formset.readonly and not formset.queryset.exists %}
{% else %}
<tr>
{% for field in formset.forms.0 %}
{% if field.label and not field|is_checkbox and not field.is_hidden %}
<th for="{{ field.auto_id }}" class="form-control-label {% if field.field.required %}requiredField{% endif %}">
{{ field.label|safe }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
</thead>
<tbody>
{% for form in formset %}
{% if form_show_errors and not form.is_extra %}
{% include "bootstrap4/errors.html" %}
{% endif %}
<tr>
{% for field in form %}
{% include 'bootstrap4/field.html' with tag="td" form_show_labels=False %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% include "bootstrap4/inputs.html" %}
{% if formset_tag %}</form>{% endif %}
{% endspecialspaceless %}

Openstack TemplateSyntaxError at /admin/networks/

There is a TemplateSyntaxError in horizon. I don not change anything in this model. All of the code is:
{% load i18n %}
{% with table.needs_form_wrapper as needs_form_wrapper %}
<div class="table_wrapper">
{% if needs_form_wrapper %}<form action="{{ table.get_absolute_url }}" method="POST">{% csrf_token %}{% endif %}
{% with columns=table.get_columns rows=table.get_rows %}
{% block table %}
<table id="{{ table.name }}" class="table table-bordered table-striped datatable">
<thead>
{% block table_caption %}
<tr class='table_caption'>
<th class='table_header' colspan='{{ columns|length }}'>
<h3 class='table_title'>{{ table }}</h3>
{{ table.render_table_actions }}
</th>
</tr>
{% endblock table_caption %}
{% block table_breadcrumb %}
{% if table.breadcrumb %}
<tr>
<td class="breadcrumb_td" colspan="{{ table.get_columns|length }}">
{{ table.breadcrumb.render }}
</td>
</tr>
{% endif %}
{% endblock table_breadcrumb %}
{% block table_columns %}
{% if not table.is_browser_table %}
<tr>
{% for column in columns %}
<th {{ column.attr_string|safe }}>{{ column }}</th>
{% endfor %}
</tr>
{% endif %}
{% endblock table_columns %}
</thead>
{% block table_body %}
<tbody>
{% for row in rows %}
{{ row.render }}
{% empty %}
<tr class="{% cycle 'odd' 'even' %} empty">
<td colspan="{{ table.get_columns|length }}">{{ table.get_empty_message }}</td>
</tr>
{% endfor %}
</tbody>
{% endblock table_body %}
{% block table_footer %}
{% if table.footer %}
<tfoot>
{% if table.needs_summary_row %}
<tr class="summation">
{% for column in columns %}
{% if forloop.first %}
<td>{% trans "Summary" %}</td>
{% else %}
<td>{{ column.get_summation|default_if_none:"–"}}</td>
{% endif %}
{% endfor %}
</tr>
{% endif %}
<tr>
<td colspan="{{ table.get_columns|length }}">
<span class="table_count">{% blocktrans count counter=rows|length %}Displaying {{ counter }} item{% plural %}Displaying {{ counter }} items{% endblocktrans %}</span>
{% if table.has_more_data %}
<span class="spacer">|</span>
More »
{% endif %}
</td>
</tr>
</tfoot>
{% endif %}
{% endblock table_footer %}
</table>
{% endblock table %}
{% endwith %}
{% if needs_form_wrapper %}</form>{% endif %}
</div>
{% endwith %}
error at " {% with columns=table.get_columns rows=table.get_rows %} ". Is there anything wrong ??? My django version is 1.4.8

Categories