Skip to content

Commit 041e7b4

Browse files
committed
Add Bootstrap template and fix inconsistancy in current template (#149)
While I was at it: - Clean up unused imports - Split templatetags tests from template tests - fix example project for django 1.7 and 1.8
1 parent 9af001d commit 041e7b4

File tree

22 files changed

+366
-213
lines changed

22 files changed

+366
-213
lines changed

django_tables2/columns/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# coding: utf-8
22
from __future__ import absolute_import, unicode_literals
33

4-
import warnings
54
from collections import OrderedDict
65
from itertools import islice
76

django_tables2/columns/checkboxcolumn.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# coding: utf-8
22
from __future__ import absolute_import, unicode_literals
33

4-
import warnings
5-
64
from django.utils.safestring import mark_safe
75

86
from django_tables2.utils import AttributeDict
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.table-container th.asc:after {
2+
content: '\0000a0\0025b2';
3+
float: right;
4+
}
5+
.table-container th.desc:after {
6+
content: '\0000a0\0025bc';
7+
float: right;
8+
}

django_tables2/tables.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import unicode_literals
33

44
import copy
5-
import warnings
65
from collections import OrderedDict
76

87
import six
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{% load querystring from django_tables2 %}
2+
{% load i18n %}
3+
{% load blocktrans trans from i18n %}
4+
5+
<div class="table-container">
6+
{% block table %}
7+
<table class="table table-bordered table-striped"{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
8+
{% block table.thead %}
9+
<thead>
10+
<tr>
11+
{% for column in table.columns %}
12+
{% if column.orderable %}
13+
<th {{ column.attrs.th.as_html }}>
14+
<a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a>
15+
</th>
16+
{% else %}
17+
<th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
18+
{% endif %}
19+
{% endfor %}
20+
</tr>
21+
</thead>
22+
{% endblock table.thead %}
23+
{% block table.tbody %}
24+
<tbody>
25+
{% for row in table.page.object_list|default:table.rows %} {# support pagination #}
26+
{% block table.tbody.row %}
27+
<tr class="{% cycle "odd" "even" %}">
28+
{% for column, cell in row.items %}
29+
<td {{ column.attrs.td.as_html }}>{{ cell }}</td>
30+
{% endfor %}
31+
</tr>
32+
{% endblock table.tbody.row %}
33+
{% empty %}
34+
{% block table.tbody.empty_text %}
35+
{% trans "no results" as table_empty_text %}
36+
<tr>
37+
<td colspan="{{ table.columns|length }}">{{ table.empty_text|default:table_empty_text }}</td>
38+
</tr>
39+
{% endblock table.tbody.empty_text %}
40+
{% endfor %}
41+
</tbody>
42+
{% endblock table.tbody %}
43+
{% block table.tfoot %}
44+
<tfoot></tfoot>
45+
{% endblock table.tfoot %}
46+
</table>
47+
{% endblock table %}
48+
49+
{% if table.page and table.paginator.num_pages > 1 %}
50+
{% block pagination %}
51+
<ul class="pager">
52+
{% if table.page.has_previous %}
53+
<li class="previous">
54+
<a href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}" class="btn btn-default">
55+
{% trans 'previous' %}
56+
</a>
57+
{% endif %}
58+
<li class="cardinality">
59+
{% blocktrans with table.page.number as current and table.paginator.num_pages as total %}
60+
Page {{ current }} of {{ total }}
61+
{% endblocktrans %}
62+
</li>
63+
{% if table.page.has_next %}
64+
<li class="next">
65+
<a href="{% querystring table.prefixed_page_field=table.page.next_page_number %}" class="btn btn-default">
66+
{% trans 'next' %}
67+
</a>
68+
</li>
69+
{% endif %}
70+
</ul>
71+
72+
{% endblock pagination %}
73+
{% endif %}
74+
</div>

django_tables2/templates/django_tables2/table.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{% spaceless %}
22
{% load django_tables2 %}
33
{% load i18n %}
4-
{% if table.page %}
4+
55
<div class="table-container">
6-
{% endif %}
76
{% block table %}
87
<table{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
98
{% nospaceless %}
@@ -70,6 +69,6 @@
7069
{% endblock pagination %}
7170
{% endwith %}
7271
{% endwith %}
73-
</div>
7472
{% endif %}
73+
</div>
7574
{% endspaceless %}

django_tables2/templatetags/django_tables2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
from __future__ import absolute_import, unicode_literals
33

44
import re
5-
import tokenize
65
from collections import OrderedDict
76

87
import six
98
from django import template
109
from django.core.exceptions import ImproperlyConfigured
11-
from django.template import Node, TemplateSyntaxError, Variable
10+
from django.template import Node, TemplateSyntaxError
1211
from django.template.defaultfilters import title as old_title
1312
from django.template.defaultfilters import stringfilter
1413
from django.template.loader import get_template, select_template

django_tables2/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import absolute_import, unicode_literals
33

44
import inspect
5-
import warnings
65
from functools import total_ordering
76
from itertools import chain
87

docs/conf.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# coding: utf-8
22
import os
3-
from os.path import abspath, dirname, join
43
import re
54
import sys
5+
from os.path import abspath
66

77
os.environ["DJANGO_SETTINGS_MODULE"] = "example.settings"
88

99
# import project
1010
sys.path.insert(0, abspath('..'))
11-
import example
12-
import django_tables2
1311
sys.path.pop(0)
1412

1513

example/app/tables.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
22
import django_tables2 as tables
3-
from .models import Country
3+
4+
from .models import Country, Person
45

56

67
class CountryTable(tables.Table):
@@ -17,3 +18,9 @@ class Meta:
1718
class ThemedCountryTable(CountryTable):
1819
class Meta:
1920
attrs = {'class': 'paleblue'}
21+
22+
23+
class BootstrapTable(tables.Table):
24+
class Meta:
25+
model = Person
26+
template = 'django_tables2/bootstrap.html'

example/app/views.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
from django_tables2 import RequestConfig, SingleTableView
55

66
from .models import Country, Person
7-
from .tables import CountryTable, ThemedCountryTable
7+
from .tables import BootstrapTable, CountryTable, ThemedCountryTable
8+
9+
try:
10+
from django.utils.lorem_ipsum import words
11+
except ImportError:
12+
# django 1.7 has lorem_ipsum in contrib.webdisign, moved in 1.8
13+
from django.contrib.webdesign.lorem_ipsum import words
814

915

1016
def multiple(request):
@@ -35,6 +41,20 @@ def multiple(request):
3541
})
3642

3743

44+
def bootstrap(request):
45+
'''Demonstrate the use of the bootstrap template'''
46+
# create some fake data to make sure we need to paginate
47+
if Person.objects.all().count() < 50:
48+
Person.objects.create_bulk([Person(name=words(3, common=False)) for i in range(50)])
49+
50+
table = BootstrapTable(Person.objects.all())
51+
RequestConfig(request, paginate={"per_page": 10}).configure(table)
52+
53+
return render(request, 'bootstrap_template.html', {
54+
'table': table
55+
})
56+
57+
3858
class ClassBased(SingleTableView):
3959
table_class = ThemedCountryTable
4060
queryset = Country.objects.all()

example/settings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# coding: utf-8
2-
# import django_tables2
3-
import sys
42
from os.path import abspath, dirname, join
53

64
from django import VERSION
@@ -97,6 +95,7 @@
9795
)
9896

9997
TEMPLATE_CONTEXT_PROCESSORS = [
98+
'django.contrib.auth.context_processors.auth',
10099
'django.core.context_processors.static',
101100
'django.core.context_processors.request'
102101
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{% load render_table from django_tables2 %}
2+
<!doctype html>
3+
<html>
4+
<head>
5+
<title>django_tables2 with bootstrap template example</title>
6+
7+
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
8+
<link href="{{ STATIC_URL }}django_tables2/bootstrap.css" rel="stylesheet" />
9+
</head>
10+
<body>
11+
<div class="container">
12+
13+
<h3>django_tables2 with bootstrap template example</h3>
14+
15+
<div class="row">
16+
<div class="col-sm-10">
17+
{% render_table table %}
18+
</div>
19+
</div>
20+
</div>
21+
</body>
22+
</html>

example/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from django.contrib import admin
55
from django.views import static
66

7-
from app.views import class_based, multiple, tutorial
7+
from app.views import bootstrap, class_based, multiple, tutorial
88

99
admin.autodiscover()
1010

1111
urlpatterns = [
1212
url(r'^$', multiple),
1313
url(r'^class-based/$', class_based),
1414
url(r'^tutorial/$', tutorial),
15+
url(r'^bootstrap/$', bootstrap),
1516

1617
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
1718
url(r'^admin/', include(admin.site.urls)),

tests/app/models.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010

1111
try:
1212
# Django >= 1.7
13-
from django.contrib.contenttypes.fields import (GenericForeignKey,
14-
GenericRelation)
13+
from django.contrib.contenttypes.fields import GenericForeignKey
14+
1515
except ImportError:
1616
# Django < 1.7, TODO: remove if we drop support for 1.7
17-
from django.contrib.contenttypes.generic import (GenericForeignKey,
18-
GenericRelation)
17+
from django.contrib.contenttypes.generic import GenericForeignKey
1918

2019

2120
class Person(models.Model):

tests/app/settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
31
import six
42
from django import VERSION
53
from django.conf import global_settings

tests/columns/test_checkboxcolumn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import django_tables2 as tables
66

7-
from ..utils import attrs, warns
7+
from ..utils import attrs
88

99

1010
def test_new_attrs_should_be_supported():

tests/columns/test_general.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import django_tables2 as tables
1010

1111
from ..app.models import Person
12-
from ..utils import build_request, parse, warns
12+
from ..utils import build_request, parse
1313

1414
request = build_request('/')
1515

tests/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import django_tables2 as tables
1313
from django_tables2.tables import DeclarativeColumnsMetaclass
1414

15-
from .utils import build_request, warns
15+
from .utils import build_request
1616

1717
request = build_request('/')
1818

0 commit comments

Comments
 (0)