如何在django中创建功能以向评论添加评论

时间:2019-04-02 16:12:58

标签: python django python-3.6

我在Django上创建了一个网站,用户可以发表评论,而Ive试图这样做,以便他们可以添加评论,但是他们添加评论只是将其保存到数据库中,而不是将其添加到帖子中。如果有人知道我该如何更改代码,以便将注释添加到他们发送消息的评论中,而不是根本不出现在网站上。

views.py:

    from django.shortcuts import render, redirect #Render is used to show the HTML, and redirect is used when the user needs to be sent to a different page automatically.
        from .models import Review #This imports the Review database.
        from .forms import ReviewForm, CommentForm #This imports the comments and review form.
        from django.utils import timezone 
        from django.contrib.auth.decorators import login_required #This requires the user to be logged in.

        def review(request):
            return render(request, 'review/review.html') 

        @login_required(login_url='/accounts/login/') 
        def review_new(request): 
            if request.method == "POST":
                form = ReviewForm(request.POST) 
                if form.is_valid(): 
                    post = form.save(commit=False) 
                    post.date = timezone.now() 
                    post.author = request.user 
                    post.save() 
                    return redirect('/review') 
            else: 
                form=ReviewForm() 
            return render(request, 'review/post_edit.html', {'form': form}) 

        def add_comment_to_post(request): 
            if request.method == "POST": 
                form = CommentForm(request.POST) 
                    comment = form.save(commit=False) 
                    comment.Review = review 
                    comment.save() #This will save the comment to the database.
                    return redirect('reivew': review) 
            else:
                form = CommentForm() 
            return render(request, 'review/add_comment_to_post.html', {'form': form}) 

models.py:

        from django.db import models 
        from django.conf import settings 
        from django.utils import timezone 

        class Review(models.Model): 
            title = models.CharField(max_length=140) 
            body = models.TextField(max_length=3000) 
            date = models.DateTimeField()
            author=models.ForeignKey(settings.AUTH_USER_MODEL, `on_delete=models.CASCADE, null=True)` 

        class comment(models.Model): 
            review = models.ForeignKey('review.Review', on_delete=models.CASCADE, related_name='comments', null=True) 
            author=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True) 
            text = models.TextField(max_length=3000) 
            created_date = models.DateTimeField(default=timezone.now)

            def __str__(self):
                return self.text

urls.py:

from django.urls import path, include, re_path 
from django.views.generic import ListView, DetailView 
from review.models import Review 
from . import views 

urlpatterns = [ 
    path('', ListView.as_view( 
        queryset=Review.objects.all().order_by("-date")[:25], 
        template_name="review/review.html")),
    re_path('(?P<pk>(\d+))',DetailView.as_view(model=Review, template_name="review/post.html")), 
    path('new/', views.review_new, name='review_new'), 
    path('new/comment', views.add_comment_to_post, name='add_comment_to_post'), 

]

1 个答案:

答案 0 :(得分:0)

我看到您应该将comment.Review = review更改为comment.review = Review.objects.get(id=review_id),其中review_id是从表单传递的ID。还要注意,您的Comment模型允许评论为空,这意味着当前您在添加评论时未将其正确链接到应与之相关的原始帖子/评论。