【问题标题】:How to avoid to resend data and update page in django forms?如何避免在 django 表单中重新发送数据和更新页面?
【发布时间】:2020-12-16 10:46:36
【问题描述】:

我有一个模态表单,单击某些按钮会显示该表单。 当用户填写表单并提交时,我可以保存数据并将用户重定向到索引页面。 如果用户返回上一页,他会看到“旧”表单已经填写,因为页面没有刷新。我能做什么?

这是我的看法:

def calendar(request):
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = ReservationForm(request.POST)
        # check whether it's valid:
        print('prima del form valid')
        if form.is_valid():
            print("il form e' valido")
            reservation = form.save(commit=False)
            reservation.validation_code = get_random_string(length=32)
            reservation.delete_code = get_random_string(length=8).upper()
            reservation.reservation_confirmed = False
            reservation.save()
            return redirect('prenotazionicampo:index')
            # return render(request, 'prenotazionicampo/index.html')
        else:
            print(form.errors)

    # if a GET (or any other method) we'll create a blank form
    else:
        form = ReservationForm()

【问题讨论】:

  • “返回”是指用户点击“返回”按钮吗?
  • 是的!浏览器的“返回”按钮

标签: python django forms redirect


【解决方案1】:

在 HTML 模板的表单中,添加 <form autocomplete="off" ...>。这应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多