未绑定方法 comment() 必须以 GetPostView 实例作为第一个参数调用(改为获取 WSGIRequest 实例)
unbound method comment() must be called with GetPostView instance as first argument (got WSGIRequest instance instead)
我在尝试以我创建的表单提交评论时遇到此错误。
这是 class 视图,带有 CommentForm
和返回 HttpResponseRedirect
的方法,仅用于测试目的:
class GetPostView(TemplateView):
template_name = 'blog/post.html'
def get(self, request, id):
return render(request, self.template_name, {
'post': Post.objects.get(pk = id),
'comments': Comment.objects.filter(post = id),
'form': CommentForm()
})
def comment(self, request):
return HttpResponseRedirect(request.path)
在这里,urls.py
:
app_name = 'blog'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name = 'index'),
url(r'^categories/$', views.CategoriesView.as_view(), name = 'categories'),
url(r'^post/(?P<id>[0-9]+)/$', views.GetPostView.as_view(), name = 'post'),
url(r'^post/(?P<id>[0-9]+)/comment$', views.GetPostView.comment)
]
而且,如题所示,当我提交表单时,出现错误:
unbound method comment() must be called with GetPostView instance as first argument (got WSGIRequest instance instead)
我是 Django 的新手,我找不到任何其他类似的情况来帮助我。
**解决方案**
我会把解决方案放在我的问题中,因为大牛值得称赞和加分。按照他的回答,我通过这样做解决了它:
"""
GetPostView
"""
class GetPostView(TemplateView):
"""
Render the view for a specific post and lists its comments
"""
template_name = 'blog/post.html'
def get(self, request, id):
return render(request, self.template_name, {
'post': Post.objects.get(pk = id),
'comments': Comment.objects.filter(post = id).order_by('-created_at'),
'form': CommentForm()
})
def write_comment(request, post_id):
"""
Write a new comment to a post
"""
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
post = Post.objects.get(pk = post_id)
post.n_comments += 1
post.save()
comment = Comment()
comment.comment = request.POST['comment']
comment.created_at = timezone.now()
comment.modified_at = timezone.now()
comment.post_id = post_id
comment.user_id = 2
comment.save()
else:
form = CommentForm()
return redirect(reverse('blog:post', args = (post_id,)))
以及新的 url
:
app_name = 'blog'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name = 'index'),
url(r'^categories/$', views.CategoriesView.as_view(), name = 'categories'),
url(r'^post/(?P<id>[0-9]+)/$', views.GetPostView.as_view(), name = 'post'),
url(r'^post/(?P<post_id>[0-9]+)/comment$', views.write_comment)
]
虽然还有很多事情可以让它变得完美,比如只允许在有用户登录时发表评论,但这是一个好的开始。
基于 Class 的视图不是那样工作的; url 需要指向从 as_view
返回的 class 本身,它们会自动分派到 get 或 post 方法,并且您不能路由到任意方法完全没有。
为您的 comment
视图定义一个单独的函数或 class。
我在尝试以我创建的表单提交评论时遇到此错误。
这是 class 视图,带有 CommentForm
和返回 HttpResponseRedirect
的方法,仅用于测试目的:
class GetPostView(TemplateView):
template_name = 'blog/post.html'
def get(self, request, id):
return render(request, self.template_name, {
'post': Post.objects.get(pk = id),
'comments': Comment.objects.filter(post = id),
'form': CommentForm()
})
def comment(self, request):
return HttpResponseRedirect(request.path)
在这里,urls.py
:
app_name = 'blog'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name = 'index'),
url(r'^categories/$', views.CategoriesView.as_view(), name = 'categories'),
url(r'^post/(?P<id>[0-9]+)/$', views.GetPostView.as_view(), name = 'post'),
url(r'^post/(?P<id>[0-9]+)/comment$', views.GetPostView.comment)
]
而且,如题所示,当我提交表单时,出现错误:
unbound method comment() must be called with GetPostView instance as first argument (got WSGIRequest instance instead)
我是 Django 的新手,我找不到任何其他类似的情况来帮助我。
**解决方案**
我会把解决方案放在我的问题中,因为大牛值得称赞和加分。按照他的回答,我通过这样做解决了它:
"""
GetPostView
"""
class GetPostView(TemplateView):
"""
Render the view for a specific post and lists its comments
"""
template_name = 'blog/post.html'
def get(self, request, id):
return render(request, self.template_name, {
'post': Post.objects.get(pk = id),
'comments': Comment.objects.filter(post = id).order_by('-created_at'),
'form': CommentForm()
})
def write_comment(request, post_id):
"""
Write a new comment to a post
"""
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
post = Post.objects.get(pk = post_id)
post.n_comments += 1
post.save()
comment = Comment()
comment.comment = request.POST['comment']
comment.created_at = timezone.now()
comment.modified_at = timezone.now()
comment.post_id = post_id
comment.user_id = 2
comment.save()
else:
form = CommentForm()
return redirect(reverse('blog:post', args = (post_id,)))
以及新的 url
:
app_name = 'blog'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name = 'index'),
url(r'^categories/$', views.CategoriesView.as_view(), name = 'categories'),
url(r'^post/(?P<id>[0-9]+)/$', views.GetPostView.as_view(), name = 'post'),
url(r'^post/(?P<post_id>[0-9]+)/comment$', views.write_comment)
]
虽然还有很多事情可以让它变得完美,比如只允许在有用户登录时发表评论,但这是一个好的开始。
Class 的视图不是那样工作的; url 需要指向从 as_view
返回的 class 本身,它们会自动分派到 get 或 post 方法,并且您不能路由到任意方法完全没有。
为您的 comment
视图定义一个单独的函数或 class。