Exemplo n.º 1
0
def edit_blog_view(request, slug):
    context = {}

    user = request.user
    if not user.is_authenticated:
        return redirect('must_authenticated')

    blog_post = get_object_or_404(BlogPost, slug=slug)

    if blog_post.author != user:
        return HttpResponse("You are not the author of this post.")

    if request.POST:
        form = UpdateBlogPostForm(request.POST or None,
                                  request.FILES or None,
                                  instance=blog_post)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.save()
            context['success_message'] = 'Updated'
            blog_post = obj

    form = UpdateBlogPostForm(
        initial={
            'title': blog_post.title,
            'body': blog_post.body,
            'image': blog_post.image,
        })
    context['form'] = form
    return render(request, 'blog/edit_blog.html', context)
def edit_blog_view(request, slug):

    context = {}
    user = request.user
    if not user.is_authenticated:
        return redirect('must_authenticate')

    blog_post = get_object_or_404(BlogPost, slug=slug)
    if request.POST:
        form = UpdateBlogPostForm(request.POST or None,
                                  request.FILES or None,
                                  instance=blog_post)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.save()
            context['success_message'] = "Updated"
            blog_post = obj

    form = UpdateBlogPostForm(
        initial={
            "title": blog_post.title,
            "body": blog_post.body,
            "image": blog_post.image,
        })
    context['form'] = form

    return render(request, 'blog/edit_blog.html', context)
Exemplo n.º 3
0
def edit_blog_view(request, slug):
    context = {}

    user = request.user
    if not user.is_authenticated:
        return redirect('account:must_authenticate')

    try:
        blog_post = get_object_or_404(BlogPost, slug=slug)
    except Http404 as e:
        return HttpResponseNotFound('Exception:{}'.format(e))

    if blog_post.author != user:
        return HttpResponseForbidden()

    if request.POST:
        form = UpdateBlogPostForm(request.POST or None,
                                  request.FILES or None,
                                  instance=blog_post)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.save()
            # blog_post = obj
            context['success_message'] = 'Successfully Updated'
            return redirect('home')
    form = UpdateBlogPostForm(
        initial={
            'title': blog_post.title,
            'body': blog_post.body,
            'image': blog_post.image,
        })

    context['form'] = form
    return render(request, 'blog/edit_blog.html', context)
Exemplo n.º 4
0
def edit_blog_view(request, slug):

    context = {}
    print(request.POST)
    user = request.user
    if not user.is_authenticated:
        return redirect("must_authenticate")

    blog_post = get_object_or_404(BlogPost, slug=slug)

    if blog_post.author != user:
        return HttpResponse("you are not the author of this blog post. ")

    img_list = Extra_Image.objects.filter(blog_id=blog_post)
    context['img_list'] = img_list

    if request.POST:
        print(request.FILES)
        form = UpdateBlogPostForm(request.POST or None,
                                  request.FILES or None,
                                  instance=blog_post)
        # formB = ImageUpdateForm()
        if form.is_valid():
            obj = form.save(commit=False)
            obj.save()
            context['success_message'] = "Updated"
            blog_post = obj

    formA = UpdateBlogPostForm(
        initial={
            "title": blog_post.title,
            "body": blog_post.body,
            "image": blog_post.image,
        })
    formB = []
    for img in img_list:

        form = ImageUpdateForm(
            initial={
                "image": img_list[0].image,
                # "body": blog_post.body,
                # "image": blog_post.image,
            })
        formB.append(form)

    # formB = [ImageUpdateForm(prefix=str(x), instance=Extra_Image()) for x in range(len(img_list))]
    # print(formB[0].initial['image'])

    context['formA'] = formA
    context['formB'] = formB
    context['slug'] = slug

    return render(request, 'blog/edit_blog.html', context)
Exemplo n.º 5
0
def edit_blog_view(request, slug):

    context = {}

    user = request.user
    if not user.is_authenticated:
        return redirect("must_authenticate")

    blog_post = get_object_or_404(BlogPost, slug=slug)

    if blog_post.author != user:
        return HttpResponse(
            "You are not the author of this post,therefore, you cannot edit it."
        )
    if blog_post.image == None:
        blog_post.image == Image.open("static/logo.png")
    else:
        blog_post.image == blog_post.image
    if request.POST:
        form = UpdateBlogPostForm(request.POST or None,
                                  request.FILES or None,
                                  instance=blog_post)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.save()
            context[
                'success_message'] = "Thanks, your edits have been updated!"
            blog_post = obj

    form = UpdateBlogPostForm(
        initial={
            "title": blog_post.title,
            "body": blog_post.body,
            "image": blog_post.image,
        })

    context['form'] = form
    return render(request, 'blog/edit_blog.html', context)
Exemplo n.º 6
0
def edit_blog_view(request, slug):

    context = {}
    user = request.user
    if not user.is_authenticated:
        return redirect("must_authenticate")

    blog_post = get_object_or_404(BlogPost, slug=slug)

    if blog_post.author != user:
        return HttpResponse("Niste autor ove objave.")

    if request.POST:
        form = UpdateBlogPostForm(request.POST or None,
                                  request.FILES or None,
                                  instance=blog_post)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.save()
            context['success_message'] = "Ažurirano."
            blog_post = obj
    form = UpdateBlogPostForm(
        initial={
            "title": blog_post.title,
            "body": blog_post.body,
            "category": blog_post.category,
            "get_category_display": blog_post.get_category_display,
            "image": blog_post.image,
            "time": blog_post.time,
            "calories": blog_post.calories,
            "ingredient_0": blog_post.ingredient_0,
            "ingredient_1": blog_post.ingredient_1,
            "ingredient_2": blog_post.ingredient_2,
            "ingredient_3": blog_post.ingredient_3,
            "ingredient_4": blog_post.ingredient_4,
            "ingredient_5": blog_post.ingredient_5,
            "ingredient_6": blog_post.ingredient_6,
            "ingredient_7": blog_post.ingredient_7,
            "ingredient_8": blog_post.ingredient_8,
            "ingredient_9": blog_post.ingredient_9,
            "ingredient_10": blog_post.ingredient_10,
            "ingredient_11": blog_post.ingredient_11,
            "ingredient_12": blog_post.ingredient_12,
            "ingredient_13": blog_post.ingredient_13,
            "ingredient_14": blog_post.ingredient_14,
            "ingredient_15": blog_post.ingredient_15,
            "ingredient_16": blog_post.ingredient_16,
            "ingredient_17": blog_post.ingredient_17,
            "ingredient_18": blog_post.ingredient_18,
            "ingredient_19": blog_post.ingredient_19,
            "step_0": blog_post.step_0,
            "step_1": blog_post.step_1,
            "step_2": blog_post.step_2,
            "step_3": blog_post.step_3,
            "step_4": blog_post.step_4,
            "step_5": blog_post.step_5,
            "step_6": blog_post.step_6,
            "step_7": blog_post.step_7,
            "step_8": blog_post.step_8,
            "step_9": blog_post.step_9,
            "step_10": blog_post.step_10,
            "step_11": blog_post.step_11,
            "step_12": blog_post.step_12,
            "step_13": blog_post.step_13,
            "step_14": blog_post.step_14,
            "step_15": blog_post.step_15,
            "step_16": blog_post.step_16,
            "step_17": blog_post.step_17,
            "step_18": blog_post.step_18,
            "step_19": blog_post.step_19,
        })
    context['form'] = form
    return render(request, 'blog/edit_blog.html', context)