Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. #Post view
  2. def post_detail(request, year, month, day, post):
  3.     post = get_object_or_404(Post, slug = post,
  4.                              status = 'published',
  5.                              publish__year = year,
  6.                              publish__month = month,
  7.                              publish__day = day)
  8.     #List of active comments for this post
  9.     comments = post.comments.filter(active=True)
  10.     sent = False
  11.     if request.method == 'POST':
  12.         #a comment was posted
  13.         comment_form = CommentForm(data=request.POST)
  14.  
  15.         if comment_form.is_valid():
  16.             #Create comment object but don't save to database yet(commit - false)
  17.             new_comment = comment_form.save(commit=False)
  18.             #Assign the current post(obj) to the comment
  19.             new_comment.post = post
  20.             #save it to the database
  21.             new_comment.save()
  22.             sent = True
  23.     else:
  24.         comment_form = CommentForm()
  25.     return render(request,
  26.                   'detail.html',
  27.                   {'post': post,
  28.                    'comments': comments,
  29.                    'comment_form': comment_form,
  30.                    'sent': sent})
  31.  
  32.  
  33. #Template
  34. ...
  35. {% if sent %}
  36.         <h2>Your comment has been added.</h2>
  37. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement