X
X
xxx2017-03-01 15:08:46
Django
xxx, 2017-03-01 15:08:46

How to save a django comment?

I want to save the comment entered in the form.
https://habrastorage.org/files/a85/f03/9b7/a85f039...
View

def valpost(request, slug = None):
  instance = get_object_or_404(Post, slug = slug)
  comment = Comment.objects.all()
  form = CommentForm(request.POST or None)

  context = {
  "title": instance.title, 
  "instance" : instance,
  "comment_form": form,
  "comments" : comment,
  }
  template = 'post_detail.html'
  return render(request, template, context)

I know what to add
if form.is_valid():
А дальше не знаю как, подскажите

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pinkevich, 2017-03-01
@Heavy10110

def valpost(request, slug = None):
    instance = get_object_or_404(Post, slug=slug)
    comments = Comment.objects.all()
    form = CommentForm()
    if request.method == 'POST'
        form = CommentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect(<success page>)
  context = {
    "title": instance.title, 
    "instance" : instance,
    "form": form,
    "comments" : comments,
  }
  template = 'post_detail.html'
  return render(request, template, context)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question