Reputation: 564
so I'm implementing commenting system. The problem I'm having is with commenting on a comment. Users are able to reply to comment, and when the user submits info to reply form and click reply button then reply is submitted to both commenting section and reply section. And when I refresh the page, that reply comment is again displayed on the commenting section. if I refresh again, then I now have three same reply on my commenting section when there should be only one reply in reply section not commenting. Sorry if this is too wordy. This is my code: 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)
and in my models.py for comment I have
def get_children(self):
if self.is_child:
return None
else:
return Comment.objects.filter(parent=self)
then 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>
I think the problem is in my views.py but not sure what's wrong....any help would be highly appreciated.
Upvotes: 0
Views: 56
Reputation: 11429
In order to prevent duplicate form submission you should implement the
Post/Redirect/Get
Pattern
So if the form submission is valid and you performed all data manipulation redirect to the empty form page:
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)
Upvotes: 1