我正在尝试使用上下文处理器构建动态侧边栏。我从数据库表中为侧边栏获取不同的值。
这是我的上下文处理器:
from clients.models import client
def sidebar(request):
return {'clientlist': client.objects.order_by('name').distinct('name')}
在views.py 我有以下代码:
from django.shortcuts import render
from django.template import loader, RequestContext
from clients.models import client
def index(request):
allclientlist = client.objects.all()
return render (request, 'clients/index.html', {'allclientlist': allclientlist}, context_instance=RequestContext(request, processors=['sidebar']))
allclientlist
用于生成包含所有客户端及其数据的表。接下来我正在尝试使用上下文处理器构建动态侧边栏并获得以下回溯
Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/root/projects/webapp/clients/views.py" in index
7. return render (request, 'clients/index.html', {'allclientlist': allclientlist}, context_instance=RequestContext(request, processors=['sidebar']))
File "/usr/local/lib/python2.7/site-packages/django/template/context.py" in __init__
179. self.update(processor(request))
Exception Type: TypeError at /clients/
Exception Value: 'str' object is not callable
当它像这样时它起作用了:
def index(request):
allclientlist = client.objects.all()
clientlist = client.objects.order_by('name').distinct('name')
return render(request, 'clients/index.html', {'allclientlist': allclientlist, 'clientlist': clientlist})
但是为了让这个菜单在所有视图中都可用,我需要clientlist
在所有视图中都有声明。我想避免这种情况并卡住了。请帮我找出这个错误。