I generate the following table using django_table2
Semester Actual Prediction
Spring 2014 209 199
*1 coe
The semester, actual, and prediction are column names. My table is exactly how i need it. However, at the bottom of each table I always have the number of items in my model. I do not want that bullet. I know its default in django_table2. Is there a way to remove this? Below is another example:
Department Semester Actual Prediction
MIE 2014 Spring 202 210
MIE 2015 Fall 213 200
MIE 2015 Spring 11 12
*3 departments
The easiest way to do it (for me) is by creating this path (your_templates_dir/django_tables2/tables.html) on your templates directory with the following content:
{% spaceless %}
{% load django_tables2 %}
{% load i18n %}
{% if table.page %}
<div class="table-container">
{% endif %}
{% block table %}
<table{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
{% nospaceless %}
{% block table.thead %}
<thead>
<tr>
{% for column in table.columns %}
{% if column.orderable %}
<th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
{% else %}
<th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
{% endif %}
{% endfor %}
</tr>
</thead>
{% endblock table.thead %}
{% block table.tbody %}
<tbody>
{% for row in table.page.object_list|default:table.rows %} {# support pagination #}
{% block table.tbody.row %}
<tr class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}"> {# avoid cycle for Django 1.2-1.6 compatibility #}
{% for column, cell in row.items %}
<td {{ column.attrs.td.as_html }}>{% if column.localize == None %}{{ cell }}{% else %}{% if column.localize %}{{ cell|localize }}{% else %}{{ cell|unlocalize }}{% endif %}{% endif %}</td>
{% endfor %}
</tr>
{% endblock table.tbody.row %}
{% empty %}
{% if table.empty_text %}
{% block table.tbody.empty_text %}
<tr><td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td></tr>
{% endblock table.tbody.empty_text %}
{% endif %}
{% endfor %}
</tbody>
{% endblock table.tbody %}
{% block table.tfoot %}
<tfoot></tfoot>
{% endblock table.tfoot %}
{% endnospaceless %}
</table>
{% endblock table %}
{% if table.page %}
{% with table.page.paginator.count as total %}
{% with table.page.object_list|length as count %}
{% block pagination %}
<ul class="pagination">
{% if table.page.has_previous %}
{% nospaceless %}{% block pagination.previous %}<li class="previous">{% trans "Previous" %}</li>{% endblock pagination.previous %}{% endnospaceless %}
{% endif %}
{% if table.page.has_previous or table.page.has_next %}
{% nospaceless %}{% block pagination.current %}<li class="current">{% blocktrans with table.page.number as current and table.paginator.num_pages as total %}Page {{ current }} of {{ total }}{% endblocktrans %}</li>{% endblock pagination.current %}{% endnospaceless %}
{% endif %}
{% if table.page.has_next %}
{% nospaceless %}{% block pagination.next %}<li class="next">{% trans "Next" %}</li>{% endblock pagination.next %}{% endnospaceless %}
{% endif %}
</ul>
{% endblock pagination %}
{% endwith %}
{% endwith %}
</div>
{% endif %}
{% endspaceless %}
This approach will override all of the tables you create via django_table2 template tag and won't necessary work with upcoming versions of django_table2, but I'll be OK if you want to keep an specific version (in my case 0.15.0).
Another approach, which is more scalable, should work with future versions (and I recommend) is by subclassing Table, like described on the django-tables2 documentation.
Related
So, I found the below Django source code file on the internet.
generic_subnavigation.html
{% load common_tags %}
{% load navigation_tags %}
{% if link|common_get_type == "<class 'mayan.apps.navigation.classes.Menu'>" %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">
{% if link.icon %}{{ link.icon.render }}{% endif %}
{{ link.label }}
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
{% navigation_resolve_menu name=link.name as sub_menus_results %}
{% for sub_menu_results in sub_menus_results %}
{% for link_group in sub_menu_results.link_groups %}
{% with '' as li_class_active %}
{% with link_group.links as object_navigation_links %}
{% include 'navigation/generic_navigation.html' %}
{% endwith %}
{% endwith %}
{% endfor %}
{% endfor %}
</ul>
</li>
{% else %}
{% if as_li %}
<li class="{% if link.active and li_class_active %}{{ li_class_active }}{% endif %}">
{% endif %}
{% include link_template|default:'navigation/generic_link_instance.html' %}
{% if as_li %}
</li>
{% endif %}
{% endif %}
The code renders a webpage with sub-menus shown in the picture below;
How do I remove the "setup and tools" menu in the sub-menus shown in the picture? At least comment on it out.
generic_link_instances.html
{% if link.separator %}
<li role="separator" class="divider"></li>
{% elif link.text_span %}
<li class="text-center link-text-span {{ link.html_extra_classes }}" >{{ link.text }}</li>
{% else %}
{% if link.disabled %}
<a class="{{ link.html_extra_classes }} {% if link_classes %}{{ link_classes }} {% else %}btn {% if 'dangerous' in link.tags %}btn-danger{% else %}btn-primary{% endif %} btn-xs{% endif %} {% if link.active %}{{ link_class_active }}{% endif %} {% if 'new_window' in link.tags %}new_window{% endif %} disabled" disabled='disabled' style="cursor: default;" href="#">{% if link.icon %}{{ link.icon.render }}{% endif %} {{ link.text }}{% if link.error %} - {{ link.error }}{% endif %}</a>
{% else %}
<a
class="{{ link.html_extra_classes }} {% if link_classes %}{{ link_classes }} {% else %}btn {% if 'dangerous' in link.tags %}btn-danger{% else %}btn-primary{% endif %} btn-xs{% endif %} {% if link.active %}{{ link_class_active }}{% endif %} {% if 'new_window' in link.tags %}new_window{% endif %}"
{% for key,value in link.html_data.items %}
data-{{ key }}={{ value }}
{% endfor %}
{% if link.html_extra_attributes %}{{ link.html_extra_attributes }}{% endif %}
{% if link.url %}href="{{ link.url }}"{% endif %}
>{% if link.icon and not hide_icon %}{{ link.icon.render }}{% endif %}
{{ link.text|default:'' }}{% if link.badge_text %} <span class="badge">{{ link.badge_text }}</span>
{% endif %}{% if link.error %} - {{ link.error }}{% endif %}
</a>
{% endif %}
{% endif %}
generic_navigation.html
{% load i18n %}
{% for link in object_navigation_links %}
{% include 'navigation/generic_subnavigation.html' %}
{% endfor %}
action_dropdown.html
{% load i18n %}
{% load common_tags %}
<button aria-expanded="false" aria-haspopup="true" class="{{ action_dropdown_classes|default:"btn btn-default btn-danger"}} btn-sm dropdown-toggle" data-toggle="dropdown" type="button">
{% if action_dropdown_label %}
{{ action_dropdown_label }}
{% else %}
{% trans 'Actions' %}
{% endif %}
<span class="caret"></span>
<span class="sr-only">{% trans 'Toggle Dropdown' %}</span>
</button>
<ul class="dropdown-menu" role="menu">
{% for menus_link_result in action_menus_link_results %}
{% if not action_menu_disable_labels and action_menus_link_results|length > 1 %}
<li class="dropdown-header"><strong>{{ menus_link_result.menu.label }}</strong></li>
<li class="divider"></li>
{% endif %}
{% for link_group in menus_link_result.link_groups %}
{% if navigation_object_list %}
{% ifchanged link_group.object %}
{% common_get_object_verbose_name obj=link_group.object as link_group_object_verbose_name %}
{% if link_group_object_verbose_name %}<li class="dropdown-header">{{ link_group_object_verbose_name }}</li>{% endif %}
{% endifchanged %}
{% endif %}
{% with link_group.links as object_navigation_links %}
{% with 'true' as as_li %}
{% with 'true' as hide_active_anchor %}
{% with 'btn btn-sm navigation-btn-dropdown' as link_classes %}
{% include 'navigation/generic_navigation.html' %}
{% endwith %}
{% endwith %}
{% endwith %}
{% endwith %}
{% if not forloop.last and link_group %}
<li class="divider"></li>
{% endif %}
{% endfor %}
{% if not forloop.last and menus_link_result %}
<li class="divider"></li>
{% endif %}
{% endfor %}
</ul>
Assume I've created a search views with chain (multiple search from different app).
I want to display my result at the same place:
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'Post' %}
{{ object.title }}
{% elif klass == 'Catego' %}
{{ object.name }}
{% elif klass == 'UserProfile' %}
{{ object.user.username }}
{% else %}
{% endif %}
{% endwith %}
{% empty %}
{% endfor %}
Everything is ok. I got all of my result.
Now, if I want to separate this result in different div (row and collapse class).
If I try this:
<div class="row collapse" id="collapseNutri">
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'Post' %}
{{ object.title }}
{% endif %}
{% endwith %}
{% endfor %}
</div>
<div class="row collapse" id="collapseCatego">
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'Catego' %}
{{ object.name }}
{% endif %}
{% endwith %}
{% endfor %}
</div>
<div class="row collapse" id="collapseUserProfile">
{% for object in object_list %}
{% with object|class_name as klass %}
{% if klass == 'UserProfile' %}
{{ object.user.username }}
{% else %}
{% endif %}
{% endwith %}
{% empty %}
{% endfor %}
</div>
I only got the Post result... I'm beginning with Django. Does anyone has an idea to separate search result in order to add different html presentation?
My child template path is project/sales/templates/sales/table.html.
It extends another child template sale_summary_change_list.html.
{% extends 'sales/sale_summary_change_list.html' %}
{% block result_list %}
<div class="results">
<table>
<thead>
<tr>
{% for header in table %}
<th>
<div class="text">
{{ header }}
</div>
</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in summary %}
<tr class="{% cycle 'row1' 'row2'}">
<td> {{ row.color_pref }} </td>
<td> {{ row.total | intcomma }} </td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
The parent template is also located in the same folder. (project/sales/templates/sales/sale_summary_change_list.html)
{% extends 'admin/change_list.html' %}
{% load humanize %}
{% block content_title %}
<h1> Sales Summary </h1>
{% endblock %}
{% block result_list %}
{% block table %} {% endblock %}
{% endblock %}
{% block pagination %}{% endblock %}
My child template however is not appearing. What am I doing wrong?
You are doing things in wrong way. Just replace {% extends 'sale_summary_change_list.html' %} this block of code with this one {% extends 'sales/sale_summary_change_list.html' %}. It may work for you.
I am having a problem and need your help.
I am making a django website, following information need to be verified. If it pass registration will be success and fail notice will show up. But Mean while there are no problem with success, but Notice won't show up if fail. Here are my form.py, could anyone help me? Much Thanks.
if settings.VERIFY_BY_SFID and all([i in cleaned_data for i in fields]):
number = cleaned_data["mobile_number"]
verified =SignUpFailureAttemptService.verify_mobile_number(number)
if not verified:
self.add_error(None, _("Your application was rejected."))
Template
{% extends 'accounts/login_base.html' %} {% load i18n %} {% load staticfiles %} {% load semanticui %} {% load common_templatetags %} {% block welcome_text %} {% trans 'Create your personal
E-wallet account.' %} {% endblock %} {% block slogan_text %} {% trans 'Buy and sell with protection
Easy and secure to send and receive money' %} {% endblock %} {% block bigger_text %} {% blocktrans %}Already have an account?{% endblocktrans %} {% trans 'Sign in' %} {% endblock %} {% block left_content %}
STEPS:
{% include 'accounts/signup/steps.html' with active_step=step steps=5 %}
{% block form_title %} {{ form_title }} {% endblock %}
{% if messages %} {% for message in messages %}
{{ message }}
{% endfor %} {% endif %}
{% block form_block %}
{% csrf_token %} {{ form.non_field_errors }} {% for hidden_field in form.hidden_fields %} {{ hidden_field.errors }} {{ hidden_field }} {% endfor %} {% for field in form.visible_fields %} {% if field.name == 'date_of_birth' %}
{{ field.label }}*
{% endif %}
{% if field.name == 'tos' %} {% blocktrans %} I agree to {% endblocktrans %} PDS and FSG {{ field }}
{% else %} {{ field }} {% endif %}
{{ field.help_text }}
{{ field.errors }}
{% endfor %}
{% if prev_step %} « {% trans 'Back' %} {% endif %}
{% if next_step_title %}
{% trans 'NEXT STEP:' %} {{ next_step_title }}
{% endif %}
{% endblock %} {% endblock %} {% block scripts_end %} {% endblock %}
I have a list of categories from DB as following and it works fine + sorted by ID.
{% for category in menu_categories|sort(attribute="id"): %}
<div>
{{ category.name }}
</div>
{% endfor %}
I just need one exception if category='Pizza' exist to list it first.
Unless I misunderstood you, this should do it:
{% for category in menu_categories|sort(attribute="id"): %}
{% if category.name == 'Pizza': %}
<div> {{ category.name }} </div>
{% endif %}
{% endfor %}
{% for category in menu_categories|sort(attribute="id"): %}
{% if category.name != 'Pizza': %}
<div> {{ category.name }} </div>
{% endif %}
{% endfor %}