Q:

Repopulating a Django form for the second time

I am making a basic blog platform with Django, I have added in an edit page where I can save a new and edited version, and have also added in a delete option. When the delete button is clicked, a new template is rendered asking for confirmation or cancellation of deletion. When I click 'yes' the post deletes itself as required, when I click 'no' it returns to the 'edit' page. The problem is that when it returns to the 'edit' page the form that is there is no longer populated, all the fields are empty. Is there any way I can configure the 'no' option to return to the 'edit' page with all the data filled in? Please advise if the question is unclear. Thank you.
views.py
def edit_post(request, slug):

    post=get_object_or_404(Blog, slug=slug)
    form = BlogForm(request.POST or None, instance=post)

    context = {
        'title': post.title,
        'post': post,
        'form': form,
        }

    if 'save' in request.POST:
        if form.is_valid():
            post=form.save(commit=False)
            post.save()
            return HttpResponseRedirect(post.get_absolute_url())

    elif 'delete' in request.POST:
        return render(request, 'Blog/confirm-deletion.html',delete_context)

    #Now delete template is shown, and view takes 'yes' or 'no' option

    elif 'yes' in request.POST:
        post.delete()
        messages.success(request, 'Post was successfully deleted')
        return HttpResponseRedirect(reverse('post_list'))

    elif 'no' in request.POST:
        form = BlogForm(request.POST, instance=post)
        context = {
            'title': post.title,
            'post': post,
            'form': form,
            }
        return render(request, 'Blog/edit_post.html', context)
        #PROBLEM HERE: CAN'T GET *NO* OPTION TO RETURN TO FILLED OUT FORM PAGE

return render(request, 'Blog/edit_post.html', context)

confirm-deletion.html
{% extends 'Blog/base.html' %}

{% block content %}
<form method='POST' action = '' enctype='multipart/form-data'> {% csrf_token%}
    <p>Are you sure you want to delete {{title}}?</p>
    <input type = 'submit' name = 'yes' value = 'Yes'/>
    <input type = 'submit' name = 'no' value = 'No'/>
</form>

{% endblock %}

forms.py
from django import forms
from .models import Blog
class BlogForm(forms.ModelForm):
    class Meta:
        model = Blog
        fields = [
        'title',
        'category',
        'content',
        'draft'
        ]

A:

When you submit a form, only the fields in that <form> tag will be submitted. For example, say you have two form tags on the page:
<form method='POST' action = '' enctype='multipart/form-data'> 
    {% csrf_token%}
    {{ form }}
    <input type='submit' name='submit' value='Submit' />
</form>

<form method='POST' action = '' enctype='multipart/form-data'> {% csrf_token%}
    <p>Are you sure you want to delete {{title}}?</p>
    <input type = 'submit' name = 'yes' value = 'Yes'/>
    <input type = 'submit' name = 'no' value = 'No'/>
</form>

When you click the 'no' button, the fields from the post form will not be submitted, because they are in a different form tag.
To submit the form data when you click 'no', you need to include the form in the same tag:
<form method='POST' action = '' enctype='multipart/form-data'> 
    {% csrf_token%}
    {{ form }}
    <input type='submit' name='submit' value='Submit' />
    <p>Are you sure you want to delete {{title}}?</p>
    <input type = 'submit' name = 'yes' value = 'Yes'/>
    <input type = 'submit' name = 'no' value = 'No'/>
</form>

