添加评论表单在尝试在 django 中添加任何评论时显示错误



更新:我在 views.py 中更改了一些代码。

基本的源文件在这里,我正在尝试包括Commment和Reply System

views.py 代码如下:

from .models import Post,Comment
from .forms import CommentForm
from django.shortcuts import redirect,render, get_object_or_404
from django.views.generic import ListView,DetailView
from django.views.generic.edit import FormMixin
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from django.views.generic.base import RedirectView

class PostList(ListView):
model=Post
template_name='home.html'
context_object_name='post_list'
queryset=Post.objects.all()
class PostDetail(DetailView):
model=Post
template_name='post_detail.html'
def get_success_url(self):
return reverse_lazy('post_detail', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(PostDetail, self).get_context_data(**kwargs)
context['comments']=Comment.objects.filter(post=self.object)
context['comment_form']=CommentForm()
return context

def post(self, request, *args, **kwargs):
self.object = self.get_object()
comment_form = self.get_form()
if comment_form.is_valid():
return self.form_valid(comment_form)
else:
return self.form_invalid(comment_form)
def form_valid(self, comment_form):
comment_form.instance.post = self.object
comment_form.save()
return super().form_valid(comment_form)

当我执行运行服务器时,它显示 **属性错误在/post/1/"PostDetail"对象没有属性"get_form" **

models.py 是:

from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
content = models.TextField()
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
name = models.CharField(max_length=80)
body = models.TextField()
reply=models.ForeignKey('Comment',on_delete=models.CASCADE,null=True)

def __str__(self):
return self.name

帖子详细信息页面显示评论和评论表单以添加评论。 但是当我尝试通过表单添加任何评论时,它显示错误。 我的想法快用完了。 如果您有任何想法,请告诉我。提前致谢

在基于类的视图中,您无需自己调用渲染函数

您放入大量代码的方法 -get_context_data- 不应返回渲染函数的输出。 它应该只返回模板的上下文字典

此外,您没有在get_context_data中指定模板,它应该是类的属性template_name='Post_detail.html'正好低于或高于model=Post

此外,当您接受表单提交时,使用FormView可能会更好。 https://docs.djangoproject.com/en/3.0/ref/class-based-views/generic-editing/#django.views.generic.edit.FormView

除此之外 - 在基于类的视图中获取请求的方法是使用self.request. 但是,如果您使用 FormView,它将处理您当前正在使用的所有位置请求。

我已经在基于类的视图中解决了它。

views.py:

from django.views.generic.edit import FormMixin
class PostDetail(DetailView,FormMixin):
model=Post
template_name='post_detail.html'
login_url='login'
form_class=CommentForm
def get_success_url(self):
return reverse_lazy('post_detail', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(BlogappDetailView, self).get_context_data(**kwargs)
context['comments']=Comment.objects.filter(post=self.object)
context['comment_form']=CommentForm()
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
comment_form = self.get_form()
if comment_form.is_valid():
return self.form_valid(comment_form)
else:
return self.form_invalid(comment_form)
def form_valid(self, comment_form):
comment_form.instance.post = self.object
comment_form.instance.author=self.request.user
comment_form.save()
return super().form_valid(comment_form)

其余部分根据模板和表单相同。

再次感谢,如果有人需要,请保留答案以备将来帮助

最新更新