表格是空的,但每次刷新时,都会提交上一个结果。

时间:2016-02-23 14:28:41

标签: python django

所以我正在实施评论系统。我遇到的问题是评论评论。用户可以回复评论,当用户提交信息以回复表单并单击回复按钮时,回复将提交给评论部分和回复部分。当我刷新页面时,该回复评论将再次显示在评论部分。如果我再次刷新,那么我现在在我的评论部分有三个相同的回复,当应该只有一个答复回复部分没有评论。对不起,如果这太罗嗦了。 这是我的代码: views.py

def post(request, slug):
        user = get_object_or_404(User,username__iexact=request.user)
        try:
            profile = MyProfile.objects.get(user_id=request.user.id)
            # if it's a OneToOne field, you can do:
            # profile = request.user.myprofile
        except MyProfile.DoesNotExist:
            profile = None


        post = get_object_or_404(Post, slug=slug)
        post.views += 1  # increment the number of views
        post.save()      # and save it

        path = request.get_full_path()
        comments = Comment.objects.filter(path=path)
        #comments = post.comment_set.all()
        comment_form = CommentForm(request.POST or None)
        if comment_form.is_valid():
            parent_id = request.POST.get('parent_id')
            parent_comment = None
            if parent_id is not None:
                try:
                    parent_comment = Comment.objects.get(id=parent_id)
                except:
                    parent_comment = None
            comment_text = comment_form.cleaned_data['comment']
            new_comment = Comment.objects.create_comment(
                user=MyProfile.objects.get(user=request.user),
                path=request.get_full_path(),
                text=comment_text,
                post = post,
                parent = parent_comment
                )

            comment_form = CommentForm()
        for c in comments:
            c.get_children()
        context_dict = {
            'post' :post,
            'profile' :profile,
            'comments' : comments,
            'comment_form':comment_form

        }

        return render(request, 'main/post.html', context_dict) 

在我的models.py中有评论我有

def get_children(self):
        if self.is_child:
            return None
        else:
            return Comment.objects.filter(parent=self)

然后post.html

<h1>Comments/Questions</h1>
<form method="POST" action=''>{% csrf_token %}
{{ comment_form.as_p }}
<input type='submit' class='btn btn-default' value='Add comment'/>
</form>


<br/>
<hr/>
<table class='table'>

{% for comment in comments %}

<tr><td>{{ comment.get_comment }} 
<br/><small>via {{ comment.user }} | {{ comment.timestamp|timesince }} ago </small>
        {% if not comment.is_child %}
        <ul>
        {% for child in comment.get_children %}
        <li>{{ child.get_comment }} 
        <small>via {{ child.user }}</small>


        </li>
        {% endfor %}
        </ul>
        <a href='#' class='reply_btn'>Reply</a>
        <div class='reply_comment'>
        <form method="POST" action=''>{% csrf_token %}
        <input type='hidden' name='parent_id' value='{{ comment.id }}' />
        {{ comment_form.as_p }}
        <input type='submit' class='btn btn-default' value='Add reply'/>
        </form>
        </div>
        {% endif %}


</td></tr>

{% endfor %}

</table>

</div>



<div class = "col-sm-3">

</div>

    {% include 'footer.html' %}



<script>
{% block jquery %}
$('.reply_btn').click(function(e){
  e.preventDefault();
  $(this).next(".reply_comment").fadeToggle();
  // $(".reply_comment").fadeToggle();
})
{% endblock %}
</script>

我认为问题出在我的views.py中,但不确定是什么问题......任何帮助都会受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

为了防止重复表单提交,您应该实现 Post/Redirect/Get Pattern

因此,如果表单提交有效,并且您执行了所有数据操作重定向到空表单页面:

def post(request, slug):
    user = get_object_or_404(User,username__iexact=request.user)
    ...
    ...
    if comment_form.is_valid():
        # do you data manipulation
        ...
        # redirect to the empty form view
        return HttpResponseRedirect('/url_of_empty_form/')
    ...

    # this will only render if the form is not valid
    return render(request, 'main/post.html', context_dict)