Пример #1
0
def comment(request, blog_name):
  usr = request.user
  contnt = request.POST['content']
  pst = Post.objects.get(id=request.POST['post_id'])
  c = Comment(user=usr, post=pst, content=contnt)
  c.save()
  return redirect('/' + blog_name + '/' + request.POST['post_id'] + '/')
Пример #2
0
    def post(self, request, slug):
        comment = request.POST.get('comment')

        print(comment)
        blog = Blog.objects.get(is_active=True, slug=slug)
        comment = Comment(blog=blog, user=self.request.user)
        comment.save()

        return render(request, self.template_name)
Пример #3
0
    def post(self, request):
        post_slug = request.POST['slug']

        name = request.POST['name']
        email = request.POST['email']
        message = request.POST['message']
        parent = request.POST['parent']

        c = Comment(name=name, email=email, message=message, parent=parent)
        c.save()

        return redirect('/blogs/' + post_slug, {'comment_added': True})
Пример #4
0
def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)
    form = CommentForm()
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment(author=form.cleaned_data['author'],
                              body=form.cleaned_data['body'],
                              post=post)
            comment.save()

    comments = Comment.objects.filter(post=post)
    context = {'post': post, 'comments': comments, 'form': form}
    return render(request, 'blog_detail.html', context)
Пример #5
0
def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)
    comments = Comment.objects.filter(post=post)

    form = CommentForm()
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment(
                author=form.cleaned_data["author"],
                body=form.cleaned_data["body"],
                post=post,
            )
            comment.save()

    context = {"post": post, "comments": comments, "form": form}
    return render(request, "blog_detail.html", context)
Пример #6
0
def blog(request, id):
    id = int(id)
    blog = Blog.objects.get(id=id) # get the id from url.
    comment = Comment.objects.filter(blog_id=id) # ??Is there any problem?

    if request.method == 'POST':
        form = UserInfo(request.POST)
        user = User(form.name, form.email, form.website)
        user.save()
    # exception
        now = datetime.now()
        comment = Comment(user, blog, form.comment, now)
        comment.save()
        c = RequestContext(request, {'blog': blog, 'comments': comment, 'form': form }) # add form
        c.update(csrf(request))
        return render_to_response('blog.html', c)
    return render_to_response('blog.html', {'blog': blog, 'comments': comment})# To invoke insert func there.
Пример #7
0
 def post(self, request, pk) :
     f = get_object_or_404(Blog, id=pk)
     comment = Comment(text=request.POST['comment'], owner=request.user, blog=f)
     comment.save()
     return redirect(reverse('blogs:blog_detail', args=[pk]))
Пример #8
0
def createComment_AJAX(request):
    if not (request.method == "POST" and request.is_ajax()):
        return JsonResponse({'message': f'Not authorized'})
    blog_id = request.POST['blog_id']
    if blog_id is None or blog_id == "":
        return JsonResponse({'message': f'Invalid Data'})
    try:
        blog = Blog.objects.get(id=int(blog_id))
    except Blog.DoesNotExist:
        return JsonResponse({'message': f'Blog does not exist'})
    content = request.POST['content']
    if content is None or content == "" or len(content) > 500:
        return JsonResponse({'message': f'Invalid content'})
    if request.user.is_authenticated:
        comment = Comment(blog=blog, user=request.user, content=content)
        comment.save()
        message = "Successfully added Comment"
        data = {
            "added":
            True,
            "message":
            message,
            "comment_html":
            render_to_string('blogs/comment.html', {
                'comment': comment,
                'request': request,
            })
        }
        return JsonResponse(data)
    else:
        name = request.POST['name']
        email = request.POST['email']
        website = request.POST['website']
        if name is None or name == "" or len(name) > 50:
            return JsonResponse({'message': f'Invalid name'})
        if email is None or email == "" or len(email) > 50:
            return JsonResponse({'message': f'Invalid email'})
        try:
            validate_email(email)
        except ValidationError as e:
            return JsonResponse({'message': f'Invalid email'})
        if len(website) > 50:
            return JsonResponse({'message': f'Invalid website'})
        comment = Comment(blog=blog,
                          name=name,
                          email=email,
                          website=website,
                          content=content)
        comment.save()
        message = "Successfully added Comment"
        data = {
            "added":
            True,
            "message":
            message,
            "comment_html":
            render_to_string('blogs/comment.html', {
                'comment': comment,
                'request': request,
            })
        }
        return JsonResponse(data)