我遇到了models.py和views.py的問題。 當用戶添加回複評論時,「Dish_comment_reply_post」總是不顯示任何內容,我需要在管理頁面中自行選擇。 請給我一個手...django外鍵的外鍵,但無法顯示在管理頁面
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})
謝謝你〜它的作品〜 –
你歡迎。 – willymwai