Django 表单在提交后保留信息

Django form keeps information after submission

我有一个 Django 表单,在提交表单时我正在尝试不同的选项来做两件事:

  1. 在没有任何信息的情况下重定向表单
  2. 提交 Django 表单后触发模式。

我已经按照这个 但是它没有用,表单已成功提交,但模式没有出现。

我在视图中添加了successful_submit的上下文,以便在提交表单后可以触发

这是 HTML 模板的一部分:

          <div class="text-center">
            <button
              class="btn btn-primary mt-5"
              onclick="stepper1.previous()"
            >
              Previous
            </button>
            <button type="submit" class="btn btn-success mt-5">
              Submit
            </button>
            <button
              type="button"
              class="btn btn-primary"
              data-mdb-toggle="modal"
              data-mdb-target="#exampleModal"
            >
              Launch demo modal
            </button>
              <!-- Modal -->
              <div
                class="modal fade"
                id="exampleModal"
                tabindex="-1"
                aria-labelledby="exampleModalLabel"
                aria-hidden="true"
              >
                <div class="modal-dialog">
                  <div class="modal-content">
                    <div class="modal-header">
                      <h5 class="modal-title" id="exampleModalLabel">
                        Modal title
                      </h5>
                      <button
                        type="button"
                        class="btn-close"
                        data-mdb-dismiss="modal"
                        aria-label="Close"
                      ></button>
                    </div>
                    <div class="modal-body">...</div>
                    <div class="modal-footer">
                      <button
                        type="button"
                        class="btn btn-secondary"
                        data-mdb-dismiss="modal"
                      >
                        Close
                      </button>
                      <button type="button" class="btn btn-primary">
                        Save changes
                      </button>
                    </div>
                  </div>
                </div>
              </div>

这里是javascript提交成功后显示的部分

    {% if successful_submit %}
        <script type="text/javascript">
            $(document).ready(function(){
                $("#exampleModal").modal('show');
            });
        </script>
    {% endif %}

这里是views.py

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', {'form': form, '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 })

在触发模态时:

add__plan(request) 的最后一行,您将 successful_submit 作为 True 传递,这应该在您首次加载页面时(在提交表单之前)进行模态加载,即模态应该在不提交表单的情况下显示。

successful_submit 最初应该是 False

def add__plan(request):
    if request.method == 'POST':
        # code
        if form.is_valid():
            # code
            return render(request, 'plan/plan.html', {'form': form, 'successful_submit': True})

    else:
        # code
    return render(request, 'plan/plan.html', {'form': form, 'successful_submit': False })

假设根据您提供的代码,您有 successful_submit=True。然后,当您加载页面时,模式应该弹出。如果模态未显示:

  • 检查浏览器控制台是否有任何错误
  • 检查 JQuery 和 Bootstrap 是否以正确的顺序加载