评论在提交表单时未注册到数据库中。

huangapple go评论68阅读模式
英文:

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 = &#39;view-post.html&#39;
    post = get_object_or_404(BlogPost, slug=slug)

    # Comment posted
    if request.method == &#39;POST&#39;:
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():

            # Create Comment object but don&#39;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(&quot;Failed to post comment&quot;)


    return redirect(&#39;viewpost&#39;, {&#39;slug&#39;: slug,})

html

&lt;div class=&quot;comments-section&quot;&gt;
    {% with post.comments.count as total_comments %}
        &lt;strong&gt;&lt;u&gt;{{ total_comments }} Comment&lt;/u&gt;&lt;/strong&gt; {{ total_comments|pluralize }}
    {% endwith %}
    {% for comment in post.comments.all %}
        {% if comment.status %}
            &lt;p&gt;{{comment.count}}&lt;/p&gt;
            &lt;p id=&quot;comment-by&quot;&gt;Comment from &lt;strong&gt;{{ comment.user}}&lt;/strong&gt; on &lt;strong&gt;&lt;i&gt;{{ comment.date }}&lt;/i&gt;&lt;/strong&gt;
            &lt;p&gt;{{ comment.text}}&lt;/p&gt;
        {% endif %}
    {% empty %}
    &lt;p style=&quot;font-weight: bold;&quot;&gt;No comments for the blog post yet, why not add one?&lt;/p&gt;
    {% endfor %}
&lt;/div&gt;
{% if user.is_authenticated %}
&lt;button class=&quot;btn btn-outline-secondary rounded-0 comment-button&quot; id=&quot;add&quot;&gt;Add Comment&lt;/button&gt;
&lt;div id=&quot;add-comment&quot;&gt;
&lt;form method=&quot;POST&quot; id=&quot;comment-form&quot;&gt;
    {% csrf_token %} 
    {{comment_form|crispy}}
    &lt;button class=&quot;btn btn-outline-secondary rounded-0 comment-button&quot; id=&quot;form-submit&quot; type=&quot;submit&quot; onClick=&quot;window.location.reload();&quot;&gt;Submit&lt;/button&gt;  
&lt;/form&gt;

答案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.

huangapple
  • 本文由 发表于 2023年2月26日 19:09:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定