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

标签 python html django django-views web-development-server

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

View .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 处理成功的表单提交防止后退按钮返回上一篇文章的模式。在您的 Django 项目中,这是通过返回 redirect 来完成的。从 View 而不是 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是一个网址。您很可能想在请求中使用相同的路径 request.path或者通过从 urls.py 文件 ( from django.shortcuts import reverse ) 中反转 url 名称来获取路径。 Reverse docs

关于python - "Confirm Form Resubmission "在我的 django 项目的 chrome 浏览器中按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67223514/

相关文章:

python - MySQL、fetchone 和 LIMIT

php - 如何从 python 调用服务器中的 php 脚本

python - 将变量值插入字符串

javascript - 防止 Element-UI 为 li 标签强制执行列表项

html - 网站对 templated.co 没有响应

python - Django导入错误: Could not import settings

python - 存储发布数据以供使用 Flask-Login 进行身份验证后使用

html - CSS:百分比宽度与固定的最小宽度相结合导致 div 不适合一行

python - 在 Django 中存储电话号码时,我应该将它们存储为原始数字还是使用 django.contrib.localflavor?

用于通用关系的 Django 表单。如何包含它们?