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
Related
I am customizing django admin template.
I can successfully remove button like (+add model) or some filter by changing overriding change_list_results.html and change_list.html
but now I want to customize each rows of the model to off the link.(I don't want to let the user go each rows editing page.)
I am checking change_list_result.html
{% load i18n static %}
{% if result_hidden_fields %}
<div class="hiddenfields">{# DIV for HTML validation #}
{% for item in result_hidden_fields %}{{ item }}{% endfor %}
</div>
{% endif %}
{% if results %}
<div class="results">
<table id="result_list">
<thead>
<tr>
{% for header in result_headers %}
<th scope="col" {{ header.class_attrib }}>
{% if header.sortable %}
{% if header.sort_priority > 0 %}
<div class="sortoptions">
<a class="sortremove" href="{{ header.url_remove }}" title="{% trans "Remove from sorting" %}"></a>
{% if num_sorted_fields > 1 %}<span class="sortpriority" title="{% blocktrans with priority_number=header.sort_priority %}Sorting priority: {{ priority_number }}{% endblocktrans %}">{{ header.sort_priority }}</span>{% endif %}
</div>
{% endif %}
{% endif %}
<div class="text">{% if header.sortable %}{{ header.text|capfirst }}{% else %}<span>{{ header.text|capfirst }}</span>{% endif %}</div>
<div class="clear"></div>
</th>{% endfor %}
</tr>
</thead>
<tbody>
{% for result in results %}
{% if result.form and result.form.non_field_errors %}
<tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td></tr>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %}">{% for item in result %}{{ item }}{% endfor %}</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
and found that <tr class="{% cycle 'row1' 'row2' %}">{% for item in result %}{{ item }}{% endfor %}</tr> out the each row.
However how can I customize each item ???
Thanks for your any help.
In django 1.7+, you can remove the links from the list from the admin model:
class UsersAdmin(admin.ModelAdmin):
list_display_links = None
However, know that this will only remove the link - it will not prevent from users getting into the view/edit page of each row if they can come up with the relevant url. For that, you'll also need to handle that view as well.
See some of these discussions here: https://stackoverflow.com/a/5837386/3121897
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 %}
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.
I have a Django project, in which one of my views is displaying a number of tables based on information stored in the database. The view is defined as follows:
def pipeline(request):
...
tables = []
def make_table(detailed_status, projects, status_view=False, first_table=False):
...
table_context_map = {
Project.ds2: {'fields': [['date added',1], ['site visit date',1], ['initial exc VAT',1]]},
...
# Similar lines to populate the tables with data from the database
...
}
table_context = table_context_map[detailed_status]
...
table_context['fields'] = [['project name',1], ['town',1], ['postcode',1], ['contact name',1]] + table_context['fields']
table = render_to_string('.../....html', table_context)
...
return render(request, 'abc.html', context)
What I'd like to do, is at a column to each table created by this view, and insert an 'autonumber' in that column for every row in the table. The tables will be populated dynamically, based on a database query whenever the view is run and the webpage loaded, I just want to number the list of items in each table as it's created.
How would I do this? I am knew to Python Django, so any help or guidance would be much appreciated.
Edit
The part of the HTML that is currently displaying these tables in the webpage looks like this:
<div class="content">
{% block tables %}
{% for table in tables %}
{# Only shows table headers on first table (css first of type on multisection thead) #}
{{table}}
{% endfor %}
{% endblock tables %}
</div>
Edit
The HTML for the file passed into the render_to_string(...) view has the following structure:
{% load getters money_handling staticfiles utilities %}
{% if projects %}
<div class="table-container m-t-lg">
<table class="multisection pipeline left">
<tr class="sub-summary">
<th colspan="4"><h3 class="p-l-sm">{{detailed_status_str}}</h3></th>
{% if total_i %}<th>Initial exc VAT: {{total_i|money:"£"}}</th>{% endif %}
{% if total_u %}<th>Latest exc VAT: {{total_u|money:"£"}}</th>{% else %}
<th></th>
{% endif %}
</tr>
</table>
<table class="multisection pipeline left m-b-xl">
<tr class="summary">
<th style="width: 3em;"></th>
{% for field in fields %}
<th class="text-sm p-l-sm p-t-sm p-b-sm" style="width:{{widths|getval:forloop.counter0}}">
{% if field.1 %}
{% if sort == field.0 and not reverse %}
{{field.0}}
{% else %}
{{field.0}}
{% endif %}
{% else %}
{{field.0}}
{% endif %}
</th>
{# Make all have the same number of columns (8) #}
{% if forloop.last %}
{% for i in ',,,,,,,,' %}
{% if forloop.counter|add:forloop.parentloop.counter0 < 11 %}
<th> </th>
{% endif %}
{% endfor %}
{% if detailed_status == "ds4"|ds %}
<th></th>
{% endif %}
{% endif %}
{% endfor %}
</tr>
{% with user.employee.full_name|is:'Nick Ross' as summary_link %}
{% for project in projects %}
<tr data-project-id="{{project.id}}" class="even {% if project.office == 2 %} col{% endif %}">
{% with initial_details=project.initial_details survey=project.survey %}
{# Open lightbox #}
<td>
{# ERF(22/11/2016 # 1450) Add a counter to display table row numbers #}
{% if user.is_superuser %}
<a class="gallery-loader" data-project-id="{{project.id}}"><i class="icon info"></i></a>
{% if forloop.first and first_table %}
<div id="iframe_gallery_wrap">
<a href="#p1" class="gallery">
<div id="p1">
<iframe class="lightbox-content" src="{% url 'projects:description' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
</div>
</a>
Probaly forloop.counter is what you are looking for.
Just use it in your template like this:
<ul>
{% for data in data_list %}
<li>{{ forloop.counter }}</li>
{% endfor %}
</ul>
As for your file hope my modifications will work (marked it with your username):
{% load getters money_handling staticfiles utilities %}
{% if projects %}
<div class="table-container m-t-lg">
<table class="multisection pipeline left">
<tr class="sub-summary">
<th colspan="4"><h3 class="p-l-sm">{{detailed_status_str}}</h3></th>
{% if total_i %}<th>Initial exc VAT: {{total_i|money:"£"}}</th>{% endif %}
{% if total_u %}<th>Latest exc VAT: {{total_u|money:"£"}}</th>{% else %}
<th></th>
{% endif %}
</tr>
</table>
<table class="multisection pipeline left m-b-xl">
<tr class="summary">
<th style="width: 3em;"></th>
<th>Number</th> #someone2088
{% for field in fields %}
<th class="text-sm p-l-sm p-t-sm p-b-sm" style="width:{{widths|getval:forloop.counter0}}">
{% if field.1 %}
{% if sort == field.0 and not reverse %}
{{field.0}}
{% else %}
{{field.0}}
{% endif %}
{% else %}
{{field.0}}
{% endif %}
</th>
{# Make all have the same number of columns (8) #}
{% if forloop.last %}
{% for i in ',,,,,,,,' %}
{% if forloop.counter|add:forloop.parentloop.counter0 < 11 %}
<th> </th>
{% endif %}
{% endfor %}
{% if detailed_status == "ds4"|ds %}
<th></th>
{% endif %}
{% endif %}
{% endfor %}
</tr>
{% with user.employee.full_name|is:'Nick Ross' as summary_link %}
{% for project in projects %}
<tr data-project-id="{{project.id}}" class="even {% if project.office == 2 %} col{% endif %}">
{% with initial_details=project.initial_details survey=project.survey %}
{# Open lightbox #}
<td>{{ forloop.counter }}</td> #someone2088
<td>
{# ERF(22/11/2016 # 1450) Add a counter to display table row numbers #}
{% if user.is_superuser %}
<a class="gallery-loader" data-project-id="{{project.id}}"><i class="icon info"></i></a>
{% if forloop.first and first_table %}
<div id="iframe_gallery_wrap">
<a href="#p1" class="gallery">
<div id="p1">
<iframe class="lightbox-content" src="{% url 'projects:description' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
</div>
</a>
I want to use django ifequal and else tag to judge if a variable equals 80 or 22. So, this is code:
{% if firewalls %}
<thead>
<tr>
<th>IP address</th>
<th>Function</th>
</tr>
</thead>
{% endif %}
<tbody>
{% for firewall in firewalls %}
<tr>
<td>{{ host_ip }} : {{ firewall.from_port }}</td>
{% ifequal firewall.to_port 22 %} <td>Ssh Service</td>
{% ifequal firewall.to_port 80 %} <td>Web Service</td>
{% else %} <td>Unknown Service</td>{% endifequal %}{% endelse %}
</tr>
{% endfor %}
And the error is Invalid block tag: 'endelse', expected 'else' or 'endifequal'. Could someone helps me? Thanks a lot!
An alternative to the ifequal tag is to use the if tag and the == operator.Bow to power of '==' operator :
{% if firewall.to_port == 20 %}
<td>Ssh Service</td>
{% elif firewall.to_port == 80 %}
<td>Web Service</td>
{% else %}
<td>Unknown Service</td>
{% endif %}
This way you are also saving your code processing time as it does not evaluate all if condition for every port number.
The equality operator is supported in Django. To solve your code issue, I'd have used :
{% if firewalls %}
<thead>
<tr>
<th>IP address</th>
<th>Function</th>
</tr>
</thead>
{% endif %}
<tbody>
{% for firewall in firewalls %}
<tr>
<td>{{ host_ip }} : {{ firewall.from_port }}</td>
{% if firewall.to_port==22 %} <td>Ssh Service</td>
{% elif firewall.to_port==80 %} <td>Web Service</td>
{% else %} <td>Unknown Service</td>{% endif %}
</tr>
{% endfor %}
{% ifequal firewall.to_port 22 %} <td>Ssh Service</td>{% endifequal %}
{% ifequal firewall.to_port 80 %} <td>Web Service</td>{% endifequal %}
{% if firewall.to_port !=22 and if firewall.to_port !=80 %} <td>Unknown Service</td>{% endif %}
for every ifequal u need to close it with endifequal u missed one