B
B
blazer052016-01-11 12:04:29
Django
blazer05, 2016-01-11 12:04:29

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)

view: display all news and comment filter
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})

view: Add comments
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)

urls:
url(r'^(?P<slug>[-\w]+)/$', views.full_slug, name='full_slug'), # Вывод новостей
url(r'^addcomment/(?P<one_id>\d+)/$', views.addcomment), # Добавление коммента

In general, in the view, I can’t prescribe a filter in this line comments = Comments.objects.filter() so that the comment is attached to the desired news. Now it turns out that all the comments come out in all the news.
I tried this:
comments = Comments.objects.filter(comments_blogpost_id=slug) but I get an error. The output should be only by id, like this
comments = Comments.objects.filter(comments_blogpost_id=one_id)
But if you use the one_id parameter instead of slug, then there will be links in the news like this full/22 not a human-readable url.
Tell me where is my mistake? I'm just learning django and may have made a bunch of mistakes and got confused.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Kuzmichev, 2016-01-11
@blazer05

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 question

Ask a Question

731 491 924 answers to any question