0

I have a Django form and I am trying show text only once the form is successfully completed

I added context so that when the form is successfull successful_submit is true and in the template I add a conditional to only show the text once it is successfully done but everytime I refresh the page or open it is showing even without submitting the form as if there is no if statement

so here is what I have done in my views:

def add__plan(request):
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = infoForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            form.save()
            _name = form.cleaned_data.get('Name')
            messages.success(request, f'PDF created for {_name}!')
            # return redirect('plan:plan')
            # redirect(reverse('plan:plan', kwargs={'successful_submit': True}))
            return render(request, 'plan/plan.html', {'successful_submit': True})

    # if a GET (or any other method) we'll create a blank form
    else:
        form = infoForm()
        print(form.errors)
    return render(request, 'plan/plan.html', {'form': form, 'successful_submit': True })

here is the text template:

              {% if successful_submit %}
              <!--Grid column-->
              <div class="col-md-3 mb-4">
                <div
                  class="toast fade show"
                  role="alert"
                  aria-live="assertive"
                  aria-atomic="true"
                >
                  <div class="toast-header">
                    <strong class="me-auto">MDBootstrap</strong>
                    <small>11 mins ago</small>
                    <button
                      type="button"
                      class="btn-close"
                      data-mdb-dismiss="toast"
                      aria-label="Close"
                    ></button>
                  </div>
                  <div class="toast-body">
                    Hello, world! This is a toast message.
                  </div>
                </div>
              </div>
              <!--Grid column-->
              {% endif %}
A_K
  • 731
  • 3
  • 15
  • 40
  • successful_submit': False.put this where you rendered the template after the else(in the get request method) . If you put True then it will render it as True )))). – Thierno Amadou Sow Oct 08 '21 at 03:28
  • `add__plan(request)` last line: `return render(request, 'plan/plan.html', {'form': form, 'successful_submit': True })`. UPDATE it to: `return render(request, 'plan/plan.html', {'form': form, 'successful_submit': False })`. – Achuth Varghese Oct 08 '21 at 18:52

0 Answers0