Answer the question
In order to leave comments, you need to log in
Can't make a bunch of comments in Django?
Hello.
Unable to properly link comments to posts. Now it turns out that the comment to one news is displayed on all other news. It is necessary that there was every comment to your news.
model:
class Comments(models.Model):
class Meta():
db_table = 'comments'
verbose_name = 'Комментарий'
verbose_name_plural = 'Комментарии'
comments_text = models.TextField(verbose_name='Текст комментария')
comments_blogpost = models.ForeignKey(BlogPost)
def full_slug(reguest, slug):
comment_form = CommentForm
form = comment_form
comments = Comments.objects.filter()
te = get_object_or_404(BlogPost, slug=slug)
return render(reguest, 'full.html', {'te': te, 'form': form, 'comments': comments, 'username': auth.get_user(reguest).username})
def addcomment(reguest, one_id):
if reguest.POST and ('pause' not in reguest.session):
form = CommentForm(reguest.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.comments_blogpost = BlogPost.objects.get(id=one_id)
form.save() # Сохраняем
return_path = reguest.META.get('HTTP_REFERER','/')
reguest.session.set_expiry(10) .
reguest.session['pause'] = True
return redirect(return_path)
url(r'^(?P<slug>[-\w]+)/$', views.full_slug, name='full_slug'), # Вывод новостей
url(r'^addcomment/(?P<one_id>\d+)/$', views.addcomment), # Добавление коммента
Answer the question
In order to leave comments, you need to log in
If we talk about the output view, then first you need to get the "blog post" itself, and then filter the comments by it, i.e. just swap 2 lines and add a filter:
def full_slug(reguest, slug):
comment_form = CommentForm
form = comment_form
te = get_object_or_404(BlogPost, slug=slug)
comments = Comments.objects.filter(comments_blogpost=te)
return render(reguest, 'full.html', {'te': te, 'form': form, 'comments': comments, 'username': auth.get_user(reguest).username})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question