-1

我有这个非常简单的 django_tables2 设置,它给出了这个错误,我不明白为什么(http://django-tables2.readthedocs.io/en/latest/pages/table-data.html#list-of-dicts):

错误:

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.

设置.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',

视图.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()

测试.html:

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

网址.py:

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

显然没有请求属性:

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

'RenderView' object has no attribute 'request'

django 2.0.2,python 3.6

4

1 回答 1

0

尝试将模板中的加载标签更改为:

{% load django_tables2 %}
{% render_table table %}
于 2018-02-28T14:59:34.213 回答