英文:
Comments not registering into database when submitting form
问题
当我尝试通过我的表单添加评论时,页面会正确重定向,但不会渲染/添加评论。
在尝试添加评论时,我在终端中收到以下错误信息:
来自('127.0.0.1',55445)的损坏管道
视图
def post_comment(request, slug):
template_name = 'view-post.html'
post = get_object_or_404(BlogPost, slug=slug)
# 发布评论
if request.method == 'POST':
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
# 创建评论对象,但暂时不保存到数据库
user_comment = comment_form.save(commit=False)
# 将当前帖子分配给评论
user_comment.post = post
# 将评论分配给用户
user_comment.user = request.user
# 将评论保存到数据库
user_comment.save()
else:
# 你可以在这里包括来自`comment_form.errors`的额外信息
messages.error("发布评论失败")
return redirect('viewpost', {'slug': slug})
HTML
<div class="comments-section">
{% with post.comments.count as total_comments %}
<strong><u>{{ total_comments }} 评论</u></strong> {{ total_comments|pluralize }}
{% endwith %}
{% for comment in post.comments.all %}
{% if comment.status %}
<p>{{comment.count}}</p>
<p id="comment-by">来自 <strong>{{ comment.user}}</strong> 于 <strong><i>{{ comment.date }}</i></strong>
<p>{{ comment.text}}</p>
{% endif %}
{% empty %}
<p style="font-weight: bold;">此博客文章暂无评论,为什么不添加一个呢?</p>
{% endfor %}
</div>
{% if user.is_authenticated %}
<button class="btn btn-outline-secondary rounded-0 comment-button" id="add">添加评论</button>
<div id="add-comment">
<form method="POST" id="comment-form">
{% csrf_token %}
{{comment_form|crispy}}
<button class="btn btn-outline-secondary rounded-0 comment-button" id="form-submit" type="submit" onClick="window.location.reload();">提交</button>
</form>
英文:
When I try add a comment via my form, the page is redirected correctly but no comment is rendered/added.
I am getting the following error in terminal when trying to add comment:
Broken pipe from ('127.0.0.1', 55445)
views
def post_comment(request, slug):
template_name = 'view-post.html'
post = get_object_or_404(BlogPost, slug=slug)
# Comment posted
if request.method == 'POST':
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
# Create Comment object but don't save to database yet
user_comment = comment_form.save(commit=False)
# Assign the current post to the comment
user_comment.post = post
# Assign comment to user
user_comment.user = request.user
# Save the comment to the database
user_comment.save()
else:
# You may include extra info here from `comment_form.errors`
messages.error("Failed to post comment")
return redirect('viewpost', {'slug': slug,})
html
<div class="comments-section">
{% with post.comments.count as total_comments %}
<strong><u>{{ total_comments }} Comment</u></strong> {{ total_comments|pluralize }}
{% endwith %}
{% for comment in post.comments.all %}
{% if comment.status %}
<p>{{comment.count}}</p>
<p id="comment-by">Comment from <strong>{{ comment.user}}</strong> on <strong><i>{{ comment.date }}</i></strong>
<p>{{ comment.text}}</p>
{% endif %}
{% empty %}
<p style="font-weight: bold;">No comments for the blog post yet, why not add one?</p>
{% endfor %}
</div>
{% if user.is_authenticated %}
<button class="btn btn-outline-secondary rounded-0 comment-button" id="add">Add Comment</button>
<div id="add-comment">
<form method="POST" id="comment-form">
{% csrf_token %}
{{comment_form|crispy}}
<button class="btn btn-outline-secondary rounded-0 comment-button" id="form-submit" type="submit" onClick="window.location.reload();">Submit</button>
</form>
答案1
得分: -1
你的表单中没有设置任何网址。我猜想你的表单只是发送到 viewpost
,而这个页面没有包含任何POST处理,所以你会再次显示相同的页面。
英文:
You do not set any url in your form. I suppose your form is just sent to viewpost
which not contains any POST process so, you display again same page.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论