调用自定义视图方法
时出现上述错误#views.py
class MyEntryAdmin(admin.ModelAdmin):
review_template = 'admin/polls/myentry/review.html'
def get_urls(self):
urls = super(MyEntryAdmin, self).get_urls()
my_urls = patterns('',
(r'\d+/review/$', self.admin_site.admin_view(self.review)),
)
return my_urls + urls
def review(self, request, id):
entry = MyEntry.objects.get(pk=id)
return render_to_response(self.review_template, {
'title': 'Review entry: %s' % entry.title,
'entry': entry,
'opts': self.model._meta,
'root_path': self.admin_site.root_path,
}, context_instance=RequestContext(request))
admin.site.register(MyEntry, MyEntryAdmin)
#urls.py
urlpatterns = patterns('polls.views'
url(r'^myentry/(?P<pk>\d+)/review/$', 'MyEntryAdmin', name='review')
)
当我调用网址http://localhost:8000/admin/polls/myentry/1/review/
时,我收到了错误
__init__() got an unexpected keyword argument 'pk'
Traceback:
Environment:
Request Method: GET
Request URL: `http://localhost:8000/admin/polls/myentry/1/review/`
Django Version: 1.6.2
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls')
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')
Traceback:
File "/home/sun1/projectFolder/myenv/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
Exception Type: TypeError at /admin/polls/myentry/1/review/
Exception Value: __init__() got an unexpected keyword argument 'pk'
答案 0 :(得分:0)
您不会将ModelAdmin类本身用作URL模式的目标。 ModelAdmin类已在现有管理员中注册,因此调用其get_urls
方法,并将相关URL添加到默认管理员。
从urls.py中删除该行,它应该可以正常工作。