当我点击 "PUBLISH COMMENT BUTTON" 时,评论不会发布在我的 Django 前端
When i hit the "PUBLISH COMMENT BUTTON" the comments does not get posted in my frontend in django
我正在 Django 中创建一个评论部分,但是当我点击评论部分时,评论确实会发布到我网站的评论部分,它只是刷新了页面,除了当我从后端添加评论时什么都不做是管理部分,它工作得很好,并在我的前端得到更新,但我博客文章详细信息中的评论表不起作用,让我展示一些我的代码
views.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']
admin.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)
models.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
此模板也呈现评论表单
<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>
views.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)
我正在 Django 中创建一个评论部分,但是当我点击评论部分时,评论确实会发布到我网站的评论部分,它只是刷新了页面,除了当我从后端添加评论时什么都不做是管理部分,它工作得很好,并在我的前端得到更新,但我博客文章详细信息中的评论表不起作用,让我展示一些我的代码
views.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']
admin.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)
models.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 此模板也呈现评论表单
<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>
views.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)