-1

I have this very simple django_tables2 setup that gives this error and I don't understand why (http://django-tables2.readthedocs.io/en/latest/pages/table-data.html#list-of-dicts):

Error:

Tag {% querystring %} requires django.template.context_processors.request to be in the template configuration in settings.TEMPLATES[]OPTIONS.context_processors) in order for the included template tags to function correctly.

settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(SETTINGS_PATH, 'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request', # <-- included
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',

views.py:

import django_tables2 as tables
from django.views.generic.base import TemplateView

class RenderView(TemplateView):
    template_name = "test.html"

    def get_context_data(self, **kwargs):
        context = super(RenderView, self).get_context_data(**kwargs)
        data = [
            {'name': 'Bradley'},
            {'name': 'Stevie'},
        ]

        table = NameTable(data)

        context["table"] = table

        return context

class NameTable(tables.Table):
    name = tables.Column()

test.html:

{% load render_table from django_tables2 %}
{% render_table table %}

urls.py:

urlpatterns = [
    path('', RenderView.as_view(), name='test'),
]

Apparently there is no request property:

def get_context_data(self, **kwargs):
    print(self.request)

gives 'RenderView' object has no attribute 'request'

django 2.0.2, python 3.6

Claudiu Creanga
  • 8,031
  • 10
  • 71
  • 110
  • Make sure you have shown your actual view, or even better, a simpler view that reproduces the problem. – Alasdair Feb 28 '18 at 15:08
  • @Alasdair that's the whole view, I added the imports now. deleted everything unrelated to the table – Claudiu Creanga Feb 28 '18 at 15:11
  • @Alasdair i was wondering if there might be related to this answer https://stackoverflow.com/questions/43592783/django-template-context-processors-request-issue-with-django-tables2-and-djang , that `request ` should be used, but unsure how to pass the request using get_context_data method, as this is the template across all site and don't want to change to render(request, ...) – Claudiu Creanga Feb 28 '18 at 15:13
  • In that question, they had to pass the request manually because they were using `template.render()` or `render_to_string`. In your case, the `TemplateView` should run the context processors and include the `request` in the template context. I can't see why you would get your error for the view you have shown. The `__init__` method is odd (I would remove it and simply write `template_name=test.html`), but I don't think that's what's causing the issue. If that's your real view, what is `context["data"] = "data"` for? – Alasdair Feb 28 '18 at 15:21
  • @Alasdair it was just to suggest that I have more things going on. I removed the context["data"] and everything else from the view... but still the same error. maybe because django2? can't see that they explicitly support django 2. – Claudiu Creanga Feb 28 '18 at 15:25
  • I'd be surprised if it was a Django 2.0 issue. I can't see any issues with your current view, and I'm not in a position to test it, so I don't have any other suggestions. Note that overriding `__init__` in class based views is unusual, usually you'd override other methods instead. – Alasdair Feb 28 '18 at 15:33
  • @Alasdair found what the issue was. I was including `{% load render_table from django_tables2 %} {% render_table table %}` inside another template tag and then inside the template through that tag. Thanks for your help! – Claudiu Creanga Feb 28 '18 at 15:33
  • So when you said that your simplified view gave the same error, you didn't actually try running the simplified view and template from your question? :( – Alasdair Feb 28 '18 at 15:35
  • @Alasdair I did run it, but forgot that test.html included in another tag and that was what gave the error. :) – Claudiu Creanga Feb 28 '18 at 15:47

1 Answers1

0

Try changing your load tags in your template to:

{% load django_tables2 %}
{% render_table table %}
morinx
  • 635
  • 7
  • 19