我正在尝试根据用户历史记录创建一个面包屑,即它会以面包屑的形式显示最后访问的x页数。
我找到了一个已完成我想要的博客(http://seancode.blogspot.co.uk/2008/10/breadcrumb-history-in-django.html)
如此习惯,我认为id将它添加到上下文处理器并将其添加到我的设置中以保存在每个视图中。
然而我得到了错误
Environment:
Request Method: GET
Request URL: http://it.internal.com/service/
Django Version: 1.9.6
Python Version: 2.7.5
Installed Applications:
('home.apps.HomeConfig',
'oncall.apps.OncallConfig',
'networks.apps.NetworksConfig',
'maintenance.apps.MaintenanceConfig',
'service.apps.ServiceConfig',
'management.apps.ManagementConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'twitter_bootstrap',
'bootstrap_pagination')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Traceback:
File "/usr/lib64/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/usr/lib64/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib64/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/var/www/infternal/service/views.py" in index
20. return render(request, 'service/index.html', {
File "/usr/lib64/python2.7/site-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/usr/lib64/python2.7/site-packages/django/template/loader.py" in render_to_string
97. return template.render(context, request)
File "/usr/lib64/python2.7/site-packages/django/template/backends/django.py" in render
95. return self.template.render(context)
File "/usr/lib64/python2.7/site-packages/django/template/base.py" in render
204. with context.bind_template(self):
File "/usr/lib64/python2.7/contextlib.py" in __enter__
17. return self.gen.next()
File "/usr/lib/python2.7/site-packages/debug_toolbar/panels/templates/panel.py" in _request_context_bind_template
81. updates.update(context)
Exception Type: ValueError at /service/
Exception Value: dictionary update sequence element #0 has length 9; 2 is required
但我不知道这意味着什么,或者如何解决它
码
# the breadcrumb history is a list of the last pages the user has visited
# the newest page is at the end of the list
# the length of the list is limited; currently it is at four pages
def breadcrumb_history(request):
history = request.session.get('breadcrumb_history', [])
# if the last item in the history is the current page, we don't want to add this page to the history
# if it's not the last item in the history, we do add it
if len(history) == 0 or history[len(history)-1] != request.path:
history.append(request.path)
# if there are more than four items in the history, pop the first one
if len(history) > 4:
history.pop(0)
# save the history to the session
request.session['breadcrumb_history'] = history
# return the current breadcrumb
return history
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'debug' : DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"django.core.context_processors.request",
"django.core.context_processors.media",
"django.core.context_processors.static",
"infternal.context_processors.breadcrumb_history",
],
},
},
]
模板:
<div id="page-content" class="">
{% if breadcrumb_history %}
<p class="breadcrumb">
{% for page in breadcrumb_history %}
<a href="{{ page }}">{{ page }}</a>
{% endfor %}
答案 0 :(得分:1)
上下文处理器总是需要返回一个字典,但是你要返回一个列表。相反,你应该这样做:
return {'history': history}