我已经阅读了django管理员视图的源代码,视图没有 接受关键字参数。
现在的问题是,如何将自己的变量传递给django的管理员 视图除了扩展django的默认视图?
我不想更改默认视图及其网址设置,我可以扩展 通过其他方式?
我编写自己的django中间件,将我的公共变量传递给我的所有视图。
但是,在它之后,当我使用djano管理员视图时,我遇到django得到了意想不到的关键字参数。
我找不到任何有关此问题的帮助,也无法从django的文档中找到示例。
以下链接是我找到的。
Django view got an unexpected keyword argument
"got an unexpected keyword argument 'ticket_id'"
我将在下面列出我的代码:
middleware.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)
blog views.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'
感谢您的帮助。
答案 0 :(得分:0)
来自django doc:
在Django调用视图之前调用process_view()。
它应该返回None或HttpResponse对象。如果它返回 没有,Django将继续处理此请求,执行任何请求 其他process_view()中间件,然后是适当的视图。如果它 返回一个HttpResponse对象,Django不会打扰任何其他对象 视图或异常中间件,或适当的视图;它适用 将中间件响应到该HttpResponse,并返回结果。
您不应该自己调用view_func,而应该将这些属性附加到请求对象中并返回None。让Django处理视图函数。返回视图调用的响应对象也会阻止其他中间件处理请求,以后可能会引发其他错误。
e.g。
def process_view(self, request, view_func, view_args, view_kwargs):
request.extra_attrs = {}
request.extra_attrs['posts_getter'] = PostsGetter()
request.extra_attrs['user_info'] = UserInfo(request)
request.extra_attrs['basic_info'] = BasicInfo(request)
request.extra_attrs['header_menu'] = HeaderMenu()
request.extra_attrs['comments_getter'] = CommentsGetter()
request.extra_attrs['tags_getter'] = TagsGetter()
request.extra_attrs['ttype_getter'] = TTypeGetter()
request.extra_attrs['link_getter'] = LinkGetter()
官方django中间件文档缺少真实的代码示例。
以下文章提供了一些例子。