How To Make Add Replies To Comments in Django?: 3 Answers
How To Make Add Replies To Comments in Django?: 3 Answers
Home
How to make add replies to comments in Django? Ask Question
PUBLIC Asked 4 years, 5 months ago Active 8 months ago Viewed 11k times
Questions
I'm making my own blog with Django and I already made a Comments system.. I want to add the
Tags The Overflow Blog
replies for each comment (like a normal comment's box) and I don't know what to do this is my
Users 5 current models.py comments: Podcast 395: Who is building clouds for
the independent developer?
COLLECTIVES
class Comment(models.Model): Exploding turkeys and how not to thaw
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments') your frozen bird: Top turkey questions...
Explore Collectives
author = models.ForeignKey(User, on_delete=models.CASCADE)
6
text = models.TextField()
FIND A JOB
created_date = models.DateField(auto_now_add=True) Featured on Meta
What is Teams?
and apparently It is working but when I try to make a comment in the admin site of Django it forces
Your privacy me to put a "Parent" to each comment (and this is not obligatory beacuse not every comment is a
By clicking “Accept all cookies”, you agree Stack Exchange can reply) I also don't know how to add the reply "button" in the HTML file. Please help tell me what Report this ad
store cookies on your device and disclose information in
accordance with our Cookie Policy. changes can I do to make a simple comment box with replies . Thanks a lot
Remote jobs
Accept all cookies python html django comments
Full Stack Developer (Django +
React)
Customize settings Share Improve this question Follow edited Jun 30 '17 at 7:03 asked Jun 30 '17 at 2:33 Self Decode No office location
badiya Deivbid $30K - $70K REMOTE
2,158 11 23 363 2 5 13 django python
ethereum bitcoin
python django
10 1. For admin site as mentioned above just set blank=True for parent field. My comment model:
python elasticsearch
def __str__(self):
return 'Comment by {}'.format(self.name)
(Senior) Frontend Engineer at
sustainable start-up (m/f/x) -
Munich or Remote
remember to run makemigrations and migrate
FINN München, Germany
€60K - €80K REMOTE RELOCATION
2.Let's start with views. I'm using the post_detail view to display the post and its comments. We
javascript reactjs
add a QuerySet to retrieve all parent active comments for this post. After this, we validate the
submitted data using the form's is_valid() . If the form is valid we check if submitted data comes
Senior WordPress Developer
from hidden input in replay button form. Next if parent_id exits we create parent Curotec No office location
object( parent_obj ) for replay comment and replay_comment object, then we assign parent_obj to REMOTE
replay_comment . If parent_obj is equal to None we just proceed with normal comment by creating html wordpress
new_comment object and saving it to the database.
what's the role of parent model field can you please explain. – Lord-shiv Jun 22 at 14:43 Is a USB security key trackable among websites?
$('#comment-form').submit(function(){
$.ajax({
type:"POST",
url:"{% url 'article_comments' article.en_title %}",
data:{"comment":$("#comment").val()},
beforeSend:function(xhr){
xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
},
success:function(data,textStatus){
$("#comment").val("");
$(".comment ul").prepend(data);
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert(XMLHttpRequest.responseText);
});
return false;
});
view.py:
print_comment = u"<p>comment:{}</p>".format(text)
if parent:
print_comment = u"<div class=\"comment-quote\">\
<p>\
<a>@{}</a>\
{}\
</p>\
</div>".format(
parent.user.username,
parent.text
) + print_comment
# current comment
html = u"<li>\
<div class=\"comment-tx\">\
<img src={} width=\"40\"></img>\
</div>\
<div class=\"comment-content\">\
<a><h1>{}</h1></a>\
{}\
<p>{}</p>\
</div>\
</li>".format(
img,
comment.user.username,
print_comment,
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
)
return HttpResponse(html)
Hi, thank you for your answe, but there's no way to make this replies without ajax or another framework? I
think that I'm getting complicated and the task is easy just add replies to comments :( – Deivbid Jun 30 '17
at 4:36
Add a comment
models.py
1 class Comment(models.Model):
author = models.CharField(max_length=100)
comment_field = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)
post = models.ForeignKey('Post', on_delete=models.CASCADE)
reply = models.ForeignKey('Comment', on_delete=models.CASCADE, related_name="replies",
def __str__(self):
return self.author
views.py
author = form.cleaned_data['author']
comment = form.cleaned_data['comment']
if reply_obj:
Comment(author=author,comment_field=comment, reply=reply_obj, post=post).sav
else:
Comment(author=author,comment_field=comment, post=post).save()
return redirect(reverse('post_detail', args=[post.slug]))
else:
form = CommentForm()
comments = Comment.objects.filter(post=post, reply=None).order_by('-date_created')
context = {
'post':post,
'form':form,
'comments':comments
}
return render(request, 'post_detail.html', context)
template (post_detail.html)
thank you very much this helped a lot. Just wanted to point out that the collapse might not work right away.
But can be easily adjusted or removed. Extremely helpful answer. Thank you! – buchstabe May 15 at 9:46
1 you're welcome. I'm happy that it helped you. – Akram Narejo May 17 at 12:41
Add a comment
Your Answer
Post Your Answer By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged python html django
STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn Instagram
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Culture & recreation
Developer Jobs Directory Advertising Work Here
Life & arts
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Professional
Mobile Terms of Service
Business
Disable Responsiveness Contact Us
Cookie Settings
API
Cookie Policy site design / logo © 2021 Stack Exchange Inc; user contributions
Data licensed under cc by-sa. rev 2021.11.25.40831