首先,这是Django 1.11.2和sekizai 0.1,这是在我的settings.py中:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS':[],
'APP_DIRS': True,
'OPTIONS': {
'builtins': ['mypage.templatetags.everywhere'],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'sekizai.context_processors.sekizai',
'django.template.context_processors.i18n',
],
},
},
]
Sekizai上下文处理器应该包含,但我仍然会收到此错误:
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/ubuntu/workspace/project/apps/django/contrib/staticfiles/handlers.py", line 63, in __call__
return self.application(environ, start_response)
File "/home/ubuntu/workspace/project/apps/django/core/handlers/wsgi.py", line 157, in __call__
response = self.get_response(request)
File "/home/ubuntu/workspace/project/apps/django/core/handlers/base.py", line 124, in get_respon e
response = self._middleware_chain(request)
File "/home/ubuntu/workspace/project/apps/django/core/handlers/exception.py", line 43, in inner
response = response_for_exception(request, exc)
File "/home/ubuntu/workspace/project/apps/django/core/handlers/exception.py", line 93, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/home/ubuntu/workspace/project/apps/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/ubuntu/workspace/project/apps/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/home/ubuntu/workspace/project/apps/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/ubuntu/workspace/project/apps/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ubuntu/workspace/hys/views/acc_settings.py", line 116, in horoscopedata
context)
File "/home/ubuntu/workspace/project/apps/django/shortcuts.py", line 21, in render_to_response
content = loader.render_to_string(template_name, context, using=using)
File "/home/ubuntu/workspace/project/apps/django/template/loader.py", line 68, in render_to_string
return template.render(context, request)
File "/home/ubuntu/workspace/project/apps/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 207, in render
return self._render(context)
File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/ubuntu/workspace/project/apps/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/ubuntu/workspace/project/apps/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/ubuntu/workspace/project/apps/classytags/core.py", line 153, in render
return self.render_tag(context, **kwargs)
File "/home/ubuntu/workspace/project/apps/sekizai/templatetags/sekizai_tags.py", line 91, in render_tag
if not validate_context(context):
File "/home/ubuntu/workspace/project/apps/sekizai/templatetags/sekizai_tags.py", line 44, in validate_context
"You must enable the 'sekizai.context_processors.sekizai' template "
TemplateSyntaxError: You must enable the 'sekizai.context_processors.sekizai' template context processor or use 'sekizai.context.SekizaiContext' to render your templates.
更令人恼火的是,特定的视图,模板和子模板不会使用或加载sekizai进行渲染。使用此渲染链中sekizai的唯一位置是在主要父级的最顶层,此特定模板扩展。
这是什么意思?我不知道如何解决这个问题。没有明显的理由说明这个位置受到影响,而且没有明确的原因,实际使用sekizai的位置也不是很好。
根据要求编辑视图:
def vvv(request):
template = "settings/template.html"
context = RequestContext(request)
if request.user.is_authenticated():
usr_status = get_user_status(request.user)
context['usr_status'] = usr_status
if model.objects.filter(user_id=request.user.id).exists():
if usr_status ==VIP:
data = model.objects.filter(user_id=request.user.id).order_by('-main')
remaining = MAX_MAIN_VIP-model.objects.filter(user_id=request.user.id).count()
else:
data = model.objects.filter(user_id=request.user.id,main=True)
remaining = MAX_MAIN_AUTH-model.objects.filter(user_id=request.user.id).filter(main=True).count()
else:
data = None
remaining = None
return render_to_response(template,
{'form':Form, 'data':data,'remaining':remaining},
context)
答案 0 :(得分:3)
您可以使用render()
快捷功能
https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#render
render(request, template[, dictionary][, context_instance][, content_type][, status][, current_app])
render()是1.3中render_to_response的一个品牌打击新快捷方式,它将自动使用我将从现在开始使用的RequestContext。
更改最后一行
return render(request, template, {'form':Form, 'data':data,'remaining':remaining, 'usr_status':usr_status})
删除行
context = RequestContext(request)
context['usr_status'] = usr_status