我遇到了models.py和views.py的问题。 当用户添加对评论的回复时,“Dish_comment_reply_post”总是不显示,我需要在管理页面中自己选择。 请帮我一把......
models.py:
class Dish_comment(models.Model):
Dish_comment_post=models.ForeignKey('dish.Dish_post', related_name='comments')
Your_name=models.CharField(max_length=200)
Content=models.TextField()
Dish_comment_created_date=models.DateField(default=timezone.now)
Dish_comment_approved=models.BooleanField(default=False)
def approve(self):
self.Dish_comment_approved=True
self.save()
def __str__(self):
return self.Content
def Dish_approved_comments(self):
return self.Dish_comment.filter(Dish_comment_approved=True)
class Dish_comment_reply(models.Model):
Dish_comment_reply_post=models.ForeignKey('dish.Dish_comment', related_name='replies', null=True)
Your_name=models.CharField(max_length=200, default='abcd12345')
Content=models.TextField()
Dish_comment_reply_created_date=models.DateField(default=timezone.now)
def __str__(self):
return self.Content
views.py:
def dish_add_comment_to_post(request,pk):
dish_post_from_db=get_object_or_404(Dish_post, pk=pk)
if request.method=="POST":
dish_comment_form=DishCommentForm(request.POST)
if dish_comment_form.is_valid():
dish_comment_from_admin=dish_comment_form.save(commit=False)
dish_comment_from_admin.Dish_comment_post=dish_post_from_db
dish_comment_from_admin.save()
return redirect('dish.views.dish_detail', pk=dish_post_from_db.pk)
else:
dish_comment_form=DishCommentForm()
return render(request, 'dish/dish_add_comment_to_post.html', {'dish_comment_form': dish_comment_form})
@login_required
def dish_comment_approve(request, pk):
dish_comment_from_db = get_object_or_404(Dish_comment, pk=pk)
dish_comment_from_db.approve()
return redirect('dish.views.dish_detail', pk=dish_comment_from_db.Dish_comment_post.pk)
@login_required
def dish_comment_remove(request, pk):
dish_comment_from_db = get_object_or_404(Dish_comment, pk=pk)
dish_post_pk = dish_comment_from_db.Dish_comment_post.pk
dish_comment_from_db.delete()
return redirect('dish.views.dish_detail', pk=dish_post_pk)
def dish_add_reply_to_comment(request, pk1, pk2):
dish_post_from_db=get_object_or_404(Dish_post, pk=pk1)
dish_comment_from_db=get_object_or_404(Dish_comment, pk=pk2)
if request.method=="POST":
dish_reply_form=DishCommentReplyForm(request.POST)
if dish_reply_form.is_valid():
dish_reply_from_admin=dish_reply_form.save(commit=False)
dish_reply_from_admin.Dish_comment_reply_post=dish_comment_from_db
dish_reply_from_admin.save()
return redirect('dish.views.dish_detail', pk=dish_post_from_db.pk)
else:
dish_reply_form=DishCommentReplyForm()
return render(request, 'dish/dish_add_reply_to_comment.html', {'dish_reply_form': dish_reply_form})
答案 0 :(得分:0)
变化
Dish_comment_post=models.ForeignKey('dish.Dish_post', related_name='comments')
来
Dish_comment_post=models.ForeignKey('Dish_post', related_name='comments')
和
Dish_comment_reply_post=models.ForeignKey('dish.Dish_comment', related_name='replies', null=True)
至Dish_comment_reply_post=models.ForeignKey('Dish_comment', related_name='replies', null=True)