gpt4 book ai didi

Django get() 正好接受 2 个参数(给定 3 个)

转载 作者:行者123 更新时间:2023-12-01 01:29:20 25 4
gpt4 key购买 nike

我正在尝试在我的测试应用程序中使用 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 时我收到以下错误

打字机错误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 里发生了什么。

最佳答案

试试这个 urlconf

from books.views import AuthorDetailView

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

并导航到:

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

关于Django get() 正好接受 2 个参数(给定 3 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5785010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com