我已经阅读了 django 管理视图的源代码,该视图不接受关键字参数。
现在的问题是,除了扩展 django 的默认视图之外,我如何将自己的变量传递给 django 的管理视图?
我不想更改默认视图及其 url 设置,我可以通过其他方式扩展它吗?
我编写自己的 django 中间件将我的公共变量传递给我的所有视图。
但是,在它之后,当我使用 djano 管理视图时,我遇到了 django 得到了意想不到的关键字参数。
我找不到有关此问题的任何帮助,也找不到 django 文档中的示例。
以下链接是我发现的。
我将在下面列出我的代码:
中间件.py
from blog.data import (UserInfo,BasicInfo,APost,HeaderMenu,PostSummary,TagsGetter,
ArchivesIndex,PostsGetter,CommentsGetter,TTypeGetter,LinkGetter)
class data_md(object):
"""pass some kwargs to view to share public data"""
def process_view(self, request, view_func, view_args, view_kwargs):
#if 'djangoadmin' not in request.path:
if True:
view_kwargs['posts_getter'] = PostsGetter()
view_kwargs['user_info'] = UserInfo(request)
view_kwargs['basic_info'] = BasicInfo(request)
view_kwargs['header_menu'] = HeaderMenu()
view_kwargs['comments_getter'] = CommentsGetter()
view_kwargs['tags_getter'] = TagsGetter()
view_kwargs['ttype_getter'] = TTypeGetter()
view_kwargs['link_getter'] = LinkGetter()
return view_func(request, view_args, view_kwargs)
博客视图.py
def tags_view(request, args, data):
tag = data.get('tagname',None)
tags_getter = data.get('tags_getter',None)
if tag in tags_getter.tagnamelist:
from blog.data import get_summarys_bytag
post_summarys = get_summarys_bytag(tag)
return render_to_response('blog/read_bytags.html',locals())
else:
raise Http404
注意:此视图运行良好。但 django 的管理视图不起作用。
错误信息:
Environment:
Request Method: GET
Request URL: http://127.0.0.1/djangoadmin/
Django Version: 1.5.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'blog')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'blog.middleware.data_md')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
109. response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/root/win7/app/django/pycms/blog/middleware.py" in process_view
20. return view_func(request, *view_args, **view_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in wrapper
219. return self.admin_view(view, cacheable)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
202. return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
Exception Type: TypeError at /djangoadmin/
Exception Value: index() got an unexpected keyword argument 'header_menu'
谢谢你的帮助。