【发布时间】:2018-08-08 11:49:40
【问题描述】:
我有一个非常简单的 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
【问题讨论】:
-
确保你展示了你的实际视图,或者更好的是,一个更简单的视图来重现问题。
-
@Alasdair 这就是整个视图,我现在添加了导入。删除了与表格无关的所有内容
-
@Alasdair 我想知道是否可能与此答案有关 stackoverflow.com/questions/43592783/… ,应该使用
request,但不确定如何使用 get_context_data 方法传递请求,因为这是跨模板所有站点并且不想更改为呈现(请求,...) -
在那个问题中,他们不得不手动传递请求,因为他们使用的是
template.render()或render_to_string。在您的情况下,TemplateView应该运行上下文处理器并将request包含在模板上下文中。我不明白为什么您会因所显示的视图而出错。__init__方法很奇怪(我会删除它并简单地写template_name=test.html),但我认为这不是导致问题的原因。如果这是你的真实想法,context["data"] = "data"是干什么用的? -
@Alasdair 只是建议我有更多事情要做。我从视图中删除了 context["data"] 和其他所有内容......但仍然是同样的错误。也许是因为 django2?看不到他们明确支持 django 2。
标签: python django python-3.x django-tables2 django-2.0