我有这个非常简单的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.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'),
]
显然没有请求属性:
def get_context_data(self, **kwargs):
print(self.request)
给出'RenderView' object has no attribute 'request'
django 2.0.2,python 3.6
答案 0 :(得分:0)
尝试将模板中的加载标记更改为:
{% load django_tables2 %}
{% render_table table %}