django_tables2给出了Tag {%querystring%}错误,尽管它包含在settings.py中

问题描述 投票:-1回答: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.

settings.朋友:

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.朋友:

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.朋友:

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

python django python-3.x django-tables2 django-2.0
1个回答
0
投票

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

{% load django_tables2 %}
{% render_table table %}
© www.soinside.com 2019 - 2024. All rights reserved.