"Confirm Form Resubmission " 在我的 django 项目的 chrome 浏览器中按回

"Confirm Form Resubmission " on pressing back in the chrome browser in my django project

这是我的第一个网络开发项目。它基本上是一个电子投票网站。索引页面是登录页面。因此,当用户使用有效凭据登录时,该网站会将用户带到投票页面,其中包含投票表格和导航栏中的一些其他选项,如反馈、新闻更新等。登录请求是“POST”。投票提交请求也是“POST”。在这里,如果用户按下其他选项,如提交反馈或查看候选人,将把用户带到相应的页面。如果用户按下后退按钮,网站应该再次将他带到投票页面,在那里他可以再次使用其他设施投票、查看候选人或提交反馈。但我面临的问题是,如果用户从第 3 页(即反馈页面或查看候选人页面)按下后退按钮,它会显示错误“确认表格重新提交”。帮我修复这个错误。提前谢谢你。

views.py

from django.shortcuts import render,redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages
from .models import voteoption,receive,received_feedback,updates

# Create your views here.
def index(request):
    return render(request,"index.html")

def login(request):
    
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']

        user = auth.authenticate(username=username,password=password)

        if user is not None:
            auth.login(request, user)
            cand = voteoption.objects.all()
            return render(request,"votepage.html", {'candidates': cand})
        else:
            messages.info(request,'Invalid Email or Password')
            return render(request,'index.html')
    else:

        return render(request,'index.html')

def logout(request):
    auth.logout(request)
    return redirect("/")

def update(request):
    news = updates.objects.all()
    return render(request,'updates.html', {'upd': news})

def contact(request):
    return render(request,'contact.html')

def feedback(request):
    return render(request,'feedback.html')

def feedbacksubmit(request):
    feedback = request.POST['feedback']
    data = received_feedback(response=feedback)
    data.save()
    messages.info(request,'Feedback Submitted Successfully')
    return render(request,'submitted.html')

def candidates(request):
    cand = voteoption.objects.all()
    return render(request,'candidates.html',{'candidates': cand} )

def submitvote(request):
    reg_no = request.POST['reg_no']
    selection = request.POST['selection'] 
    voter = request.POST['voter']

    if receive.objects.filter(mail=voter).exists():
        messages.info(request,'You have already Voted')
        return render(request,'submitted.html')
    else:
        data = receive(reg_no=reg_no,selection=selection,mail=voter)
        data.save()
        messages.info(request,'You have voted Successfully. Thank You!')
        return render(request,'submitted.html')

我应该做哪些改变?

由于这是我的第一个项目,可能会有很多编码技术不佳的地方。所以,请给我建议更好的代码,即使它与这个错误无关,我也应该在我的代码中替换它。

您将希望使用 Post/Redirect/Get pattern to prevent the back button from returning to the previous post. In your Django project this is done by returning a redirect from the view instead of an HttpResponse. Redirect docs

处理成功的表单提交
from django.shortcuts import redirect

def view_function(request, ...):
    ... # code processing the POST
    if post_request_successful:
        return redirect(path)
    else:
        return HttpResponse(...)  # or render(...) or whatever you usually return

其中 path 是 url。您很可能想在请求 request.path 中使用相同的路径,或者通过反转 urls.py 文件 (from django.shortcuts import reverse) 中的 url 名称来获取路径。 Reverse docs