繁体   English   中英

当我点击“发布评论按钮”时,评论不会发布在我的前端 django

[英]When i hit the "PUBLISH COMMENT BUTTON" the comments does not get posted in my frontend in django

我正在 django 中创建评论部分,但是当我点击评论部分时,评论确实会发布到我网站的评论部分,它只是刷新了页面并且什么都不做,但是当我从后端添加评论时,它是管理部分工作得很好,并在我的前端得到更新,但我的博客文章详细信息中的评论表单不起作用,让我展示一些我的代码

视图.py

# this view returns the blog details and the comment section with the form
def blog_detail(request, blog_slug):
    post = get_object_or_404(Blog, slug=blog_slug)
    # post = Blog.objects.filter(slug=blog_slug)
    categories = Category.objects.all()
    comments = post.comments.filter(active=True)
    new_comment = None
    if request.method == "POST":
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            new_comment.name = request.user
            new_comment.save()
    else:
        comment_form = CommentForm()

    context = {
        'post': post,
        'comments': comments,
        'comment_form': comment_form,
        'new_comment': new_comment,
        'categories': categories,
    }
    return render(request, 'blog/blog-details.html', context)

forms.py

class CommentForm(forms.ModelForm):
    # tags = forms.CharField(widget=forms.TextInput(attrs={'class': 'input is-medium'}), required=True)
    
    class Meta:
        model = Comment
        fields = ['email', 'body']

管理员.py

@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ('name', 'body', 'post', 'created_on')
    list_filter = ('active', 'created_on')
    search_fields = ['approve_comment']

    def approve_comment(self, request, queryset):
        queryset.update(active=True)

模型.py

class Comment(models.Model):
    post = models.ForeignKey(Blog, on_delete=models.CASCADE, related_name='comments')
    name = models.ForeignKey(User, on_delete=models.DO_NOTHING, verbose_name="Name")
    email = models.EmailField()
    body = models.TextField(verbose_name="Write Comment")
    created_on = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)

    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return 'Comment: {} by {}'.format(self.body, self.name)

blogdetail.html 这个模板也呈现评论 forms

<div class="comment-form">
                                            <form action="#">
                                                <div class="row">
                                                    {% if new_comment %}
                                                    <div class="alert alert-success" role="alert">
                                                        Your comment is awating approval
                                                    </div>
                                                    {% else %}
                                                    <form method="POST">
                                                        {% csrf_token %}
                                                        {{comment_form|crispy}} <br>
                                                        <button type="submit">Post Comment</button>
                                                        
                                                    </form>
                                                    {% endif %}
                                                </div>
                                            </form>
                                        </div>

我已经尝试了很多方法来解决这个问题,但它最终无法正常工作,请注意我没有收到任何错误,但它只是刷新页面,然后没有评论出现。

任何帮助将不胜感激

模板

<div class="comment-form">
                                            
                                                <div class="row">
                                                    {% if new_comment %}
                                                    <div class="alert alert-success" role="alert">
                                                        Your comment is awating approval
                                                    </div>
                                                    {% else %}
                                                    <form action="" method="POST">
                                                        {% csrf_token %}
                                                        {{comment_form|crispy}} <br>
                                                        <button type="submit">Post Comment</button>
                                                        
                                                    </form>
                                                    {% endif %}
                                                </div>
                                           
                                        </div>

视图.py

def blog_detail(request, blog_slug):
    post = get_object_or_404(Blog, slug=blog_slug)
    # post = Blog.objects.filter(slug=blog_slug)
    categories = Category.objects.all()
    comments = post.comments.filter(active=True)
    new_comment = post.comments.filter(active=False,name=request.user)
    if request.method == "POST":
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            new_comment.name = request.user
            new_comment.active = False #this make the comment non active until it is approuved by admin
            new_comment.save()
            return redirect('the-url-of-the-blog-detail')

    else:
        comment_form = CommentForm()

    context = {
        'post': post,
        'comments': comments,
        'comment_form': comment_form,
        'new_comment': new_comment,
        'categories': categories,
    }
    return render(request, 'blog/blog-details.html', context)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM