def newComment(request, pk): blog = get_object_or_404(Blog, pk=pk) # If this is a POST request then process the Form data if request.method == 'POST': # Create a form instance and populate it with data from the request (binding): form = NewCommentForm(request.POST) #Check if the form is valid: if form.is_valid(): # process the data in form.cleaned_data as required (here we just write it to the model due_back field) new_comment = Comment.objects.create( blog=blog, comment=form.cleaned_data['comment'], author=request.user) new_comment.save() # redirect to a new URL return HttpResponseRedirect(reverse('blog-detail', args=[str(pk)])) # If this is a GET (or any other method) create the default form. else: form = NewCommentForm() context = { 'form': form, 'blog': blog, } return render(request, 'blog/comment_form.html', context=context)
def new_comment(request, name, id): blog = Blog.objects.get(name = name) post = Post.objects.get(id = id) if request.method == 'POST': form = NewCommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() post.comments += 1 post.save() else: return HttpResponse("Incorrect form!!") else: form = NewCommentForm({'post':post}) all_comments = post.comment_set.all().order_by('-created') return render_to_response('blog/new_comment.html', {'form': form, 'post': post, 'blog': blog, 'comments': all_comments, }, context_instance = RequestContext(request) ) return HttpResponseRedirect(reverse('blog.views.blog', args = (blog.name, )))
def test_new_comment_form_max_over(self): sample_comment = "hello" * 1000 form = NewCommentForm(data={'comment': sample_comment}) self.assertTrue(form.is_valid())