Answer the question
In order to leave comments, you need to log in
Trying to nest comments (django)?
There is a comment, it has a reply button. Clicking opens a form with an input for a new comment and a submit button.
The idea is to add an attribute to the POST request that transmits the parent comment on the send button, and in the view accept it and save the comment with the parent.
The question is how to pass the name of the parent comment to the request?
models.py
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField('Содержание', max_length = 300, default = '')
created_date = models.DateTimeField(auto_now_add=True)
parent = models.ForeignKey(
'self',
default=None,
blank=True, null=True,
on_delete=models.CASCADE,
related_name='parent_%(class)s',
verbose_name='parent comment'
)
def __str__(self):
return self.content
class Meta:
ordering = ['-created_date']
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('content', )
def detail(request, slug):
html = 'detail.html'
post = get_object_or_404(Post, slug=slug)
if request.method == "POST":
parent = request.POST.get('parent', None)
print(parent)
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.user = request.user
comment.parent = parent
comment.save()
return redirect('detail_man_post', slug=post.slug)
else:
form = CommentForm()
<ol class="commentlist">
{% for comment in detail.comments.all %}
<li class="depth-1 comment">
<div class="comment__avatar">
<img width="50" height="50" class="avatar" src="{{ comment.user.avatar.url }}" alt="">
</div>
<div class="comment__content">
<div class="comment__info">
<cite>{{ comment.user }}</cite>
<div class="comment__meta">
<time class="comment__time">{{ comment.created_date }}</time>
<div class="clickreply" style="">
<span class="btn clickreply" id="replyb">Ответить</span>
</div>
<b> <form class = 'reply' method="POST" name="contactForm" id="contactForm" style="display:none" >
{% csrf_token %}
<div class="message form-field">
{{ form.content }}
</div>
<button type="submit" class="submit btn--primary btn--large full-width">Отправить</button>
</form>
</b>
<!-- end form -->
{% if request.user.pk == comment.user.pk %}
<a class="reply" href="{% url 'comment_remove' pk=comment.pk %}">Удалить</a>
{% endif %}
</div>
</div>
<div class="comment__text">
<p>{{ comment.content|linebreaks }}.</p>
</div>
</div>
{% if comment.parent %}
Дочерний коммент {{ comment.parent.content }} Пользователь {{ comment.parent.user }}
<ul class="children">
<li class="depth-2 comment">
<div class="comment__avatar">
<img width="50" height="50" class="avatar" src="{{ comment.parent.user.avatar.url }}" alt="">
</div>
<div class="comment__content">
<div class="comment__info">
<cite>{{ comment.parent.user }}</cite>
<div class="comment__meta">
<time class="comment__time">{{ comment.parent.created_date }}</time>
<div class="clickreply" style="">
<span class="btn clickreply" id="replyb">Ответить</span>
</div>
<<b>form class = 'reply' method="POST" name="contactForm" id="contactForm" style="display:none" >
{% csrf_token %}
<input type="hidden" name="parent" value='qweqweqwe'>
<div class="message form-field">
{{ form.content }}
</div>
<button type="submit" class="submit btn--primary btn--large full-width">Отправить</button>
</form> <!-- end form --></b>
{% if request.user.pk == comment.user.pk %}
<a class="reply" href="{% url 'comment_remove' pk=comment.pk %}">Удалить</a>
{% endif %}
</div>
</div>
<div class="comment__text">
<p>{{ comment.parent.content }}</p>
</div>
</div>
</li>
</ul>
{% endif %}
</li>
{% empty %}
<h1>Нет комментариев</h1>
</li> <!-- end comment level 1 -->
{% endfor %}
</ol>
Answer the question
In order to leave comments, you need to log in
You do not need to pass the name of the parent comment, just pass its Id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question