2

In Django 1.7, I'm unable to provide a custom template. Instead it just defaults to the django_tables2/table.html

from django_tables2 import Table as BaseTable
class Table(BaseTable):
    class Meta:
        template = 'portal/base_table.html'

And my folder structure:

apps/portal/
├── __init__.py
├── tables.py
├── templates
│   └── portal
│       ├── base.html
│       ├── base_portal.html
│       ├── base_table.html
│       └── home.html

In the above, templates such as portal/base.html are resolved by template finders.

If I forego the Meta class and instead set:

from django_tables2 import Table as BaseTable
class Table(BaseTable):
    template = 'portal/base_table.html'

I instead get the error TemplateDoesNotExist as it seems to be trying to resolve /data/www/apps/portal/templates/No template names provided among others.

To further support that I think the template should be resolving:

>>> render_to_response('portal/base_table.html')
<django.http.response.HttpResponse object at 0x7fa940c74690>

>>> render_to_response('portal/base_table.html2')
    ...
    raise TemplateDoesNotExist(name)
TemplateDoesNotExist: portal/base_table.html2

My settings.py file contains:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
path = lambda *a: os.path.join(BASE_DIR, *a)
TEMPLATE_DIRS = (
    path('templates'),
)

This is working for all other templates such as for Views. Does django-tables2 not use the same lookup method?

DanH
  • 5,498
  • 4
  • 49
  • 72

3 Answers3

0

In django tables2, for using custom template for providing custom template, you need to render it in templates like this:

<div class="">
    {% load render_table from django_tables2 %}
    {% render_table table 'portal/base_table.html'%}
</div>

You can check this answer as reference: Is it possible to apply a template tag to a <td> when using django-tables2?

Community
  • 1
  • 1
ruddra
  • 50,746
  • 7
  • 78
  • 101
0

As I ran into this Error with Django 1.8, and nobody seems to have the solution, for future reference this is how I solved it:

I had a manager that would be called as the default manager. This managers method get_queryset() was overwritten with a function to sort the resulting objects and return this list.

Turns out even though this is often seen in examples on the net and something similar is inside the documentation, you can only return a queryset there, not a list! Therefore just rename that function to anything else and you are allowed to return lists. Then you just call model.manager.anythingbutgetqueryset() and you are fine.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Trooner
  • 1
  • 3
-3

Add following setting to your settings.py file :-

TEMPLATE_DIRS = (
    "path_to_your_template_directory",
)

Edit the path accordingly

zephyr
  • 1,775
  • 6
  • 20
  • 31
  • I already have `TEMPLATE_DIRS` set, and I would have thought `render_to_response('portal/base_table.html')` being successful should indicate as such? I'll update the OP though. – DanH May 08 '15 at 08:19