Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Post view
- def post_detail(request, year, month, day, post):
- post = get_object_or_404(Post, slug = post,
- status = 'published',
- publish__year = year,
- publish__month = month,
- publish__day = day)
- #List of active comments for this post
- comments = post.comments.filter(active=True)
- sent = False
- if request.method == 'POST':
- #a comment was posted
- comment_form = CommentForm(data=request.POST)
- if comment_form.is_valid():
- #Create comment object but don't save to database yet(commit - false)
- new_comment = comment_form.save(commit=False)
- #Assign the current post(obj) to the comment
- new_comment.post = post
- #save it to the database
- new_comment.save()
- sent = True
- else:
- comment_form = CommentForm()
- return render(request,
- 'detail.html',
- {'post': post,
- 'comments': comments,
- 'comment_form': comment_form,
- 'sent': sent})
- #Template
- ...
- {% if sent %}
- <h2>Your comment has been added.</h2>
- ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement