Django评论功能异常:提交后评论无法保存问题排查
拍卖帖子评论提交后无法保存问题修复
问题现象
实现拍卖帖子的用户评论功能,评论需关联对应帖子,但用户提交评论后数据无法存入数据库,仅通过后台添加的评论能正常显示。
涉及代码排查
view.py - 页面展示与评论处理函数
# 页面展示函数 def list_page(request, list_id): if request.user.is_authenticated: list_auction = Auction_listings.objects.get(id=list_id) # 逻辑错误:用拍卖帖子ID查询分类,ID不匹配会报错 categories = Category.objects.get(id = list_id) return render(request, "auctions/auc_details.html", { "detail": list_auction, "cats":categories, "user": request.user, "comments": list_auction.comment.all(), }) else: list_auction = Auction_listings.objects.get(id =list_id) return render(request, "auctions/auc_details.html", { "detail":list_auction }) # 评论处理函数 def comment(request, list_id): if request.user.is_authenticated: list_auction = get_object_or_404(Auction_listings, pk=list_id) if request.method == "POST": comment_form = forms.Create_comment(request.POST) if comment_form.is_valid(): com_t = comment_form.save(commit=False) # 字段名错误:关联帖子的字段是comment_on而非comment com_t.comment = list_auction com_t.comment_by = request.user com_t.save() print(com_t) return HttpResponseRedirect(reverse("list_page", args=(list_auction.id,))) return render (request,"auctions/auc_details.html", { "detail": list_auction, "user": request.user, "comments": list_auction.comment.all(), })
urls.py - 路由配置
path("<int:list_id>/comment", views.comment, name="comment"),
forms.py - 评论表单类
class Create_comment(forms.ModelForm): class Meta: model = models.Comment fields = ['comment']
models.py - 数据模型
Comment模型
class Comment(models.Model): comment_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="commentor", blank=False) comment_on = models.ForeignKey(Auction_listings, related_name="comment", on_delete=models.CASCADE) comment = models.CharField(max_length=600) comment_date_published = models.DateTimeField(auto_now_add=True) def __str__(self): return f"{self.comment}"
Auction_listings模型
class Auction_listings(models.Model): auc_title = models.CharField(max_length=50) auc_details = models.CharField(max_length=250) auc_price = models.IntegerField() auc_date_published = models.DateTimeField(auto_now_add=True) auc_created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="creator", blank=True) auc_image = models.ImageField(default='rose.jpg', blank=True) auctions = models.ManyToManyField(Category, blank= True, related_name="category") def __str__(self): return f"{self.auc_title}"
HTML页面 - 评论表单
<form action="{% url 'comment' detail.id %}" method="POST"> {% csrf_token %} <!-- 字段名不匹配:input的name是text,但表单字段是comment --> <input type="text" name="text"> <input type="hidden" name="auction_id" value="{{detail.id}}"> <button type=" submit ">Create</button> </form>
核心问题与修复步骤
1. 表单输入字段不匹配
HTML表单输入框的name="text"与表单类字段comment不对应,导致提交时表单无法获取评论内容,comment_form.is_valid()验证失败,无法进入保存逻辑。
修复:
修改HTML表单的input name为comment:
<input type="text" name="comment">
2. 模型字段赋值错误
评论处理视图中,错误使用com_t.comment = list_auction关联帖子,但Comment模型中关联帖子的字段是comment_on,导致关联关系未正确设置,保存后评论无法关联到对应帖子。
修复:
将视图中的赋值语句改为:
com_t.comment_on = list_auction
3. 可选:修复页面分类查询逻辑
list_page函数用拍卖帖子ID查询分类的逻辑错误,应从拍卖帖子的auctions多对多字段获取关联分类:
修复:
# 替换原categories查询语句 categories = list_auction.auctions.all()
验证修复效果
修改完成后重新提交评论:
- 表单输入内容会被正确传递并通过验证
- 评论对象会正确关联到对应拍卖帖子
- 提交后页面重定向,新评论会正常显示在帖子详情页
内容的提问来源于stack exchange,提问作者Mohamed
相关产品推荐
相关产品推荐

