Django get()只需要2个参数(给出3个)

时间:2011-04-26 01:31:25

标签: django django-views django-1.3

我正在尝试在我的测试应用中使用DetailView泛型类,这就是我在视图中的内容

* 从此example

更新了模型*
class AuthorDetailView(DetailView):

     context_object_name = "author"
     queryset = Author.objects.all()
     slug_field = "id"


    def get_object(self):
        object = super(AuthorDetailView, self).get_object()

        return object

作为测试

在我的urls.py文件中我得到了

('^author/(\d+)/$', Author.AuthorDetailView.as_view()),

当我导航到http://localhost:8000/author/1时出现以下错误

Typer错误 get()只需要2个参数(给定3个)

Traceback:
File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.6/site-packages/django/views/generic/base.py" in view
  47.             return self.dispatch(request, *args, **kwargs)
File "/Library/Python/2.6/site-packages/django/views/generic/base.py" in dispatch
  68.         return handler(request, *args, **kwargs)

Exception Type: TypeError at /author/1/
Exception Value: get() takes exactly 2 arguments (3 given)

我真的不明白base.py中发生了什么。

2 个答案:

答案 0 :(得分:4)

试试这个urlconf

from books.views import AuthorDetailView

urlpatterns = patterns('',
    #...
    (r'^authors/(?P<pk>\d+)/$', AuthorDetailView.as_view()),
)

并导航至:

http://localhost:8000/author/1/

答案 1 :(得分:0)

我做了以下更改,并且有效。

  

将调试工具栏升级到1.5或将sqlparse降级到0.1.x。

来源:https://github.com/jazzband/django-debug-toolbar/issues/862(非常感谢github的用户aaugustin)