django防止表单数据重复提交

时间:2021-12-30 21:41:03

思路:
        GET访问时,服务器返回页面,同时生成认证token,保存入session

POST访问时,校验客户端的token与session中保存的token是否一致

具体代码:

import uuid

from django.http import HttpResponse
from django.shortcuts import render,render_to_response from DataAccess.Supplier import supplier_DAC
from common import utils
from common.Supplier import urlconfig
from Supplier.supperForm import SupplierForm
# Create your views here.
def index(request):
supplier_table_data=supplier_DAC.getAllSupplierInfo()
page_id=''
if utils.Is_GET(request):
page_id=str(uuid.uuid4())
request.session["pageid"]=page_id
return render_to_response(urlconfig.index,{'form':SupplierForm(),'data':supplier_table_data,'pageId':page_id})
elif utils.Is_POST(request):
if utils.GetData(request,'hid')==request.session["pageid"]:
form_p=SupplierForm(request.POST)
if SupplierForm.is_valid:
form_p.save()
page_id=str(uuid.uuid4())
request.session["pageid"]=page_id
return render_to_response(urlconfig.index,{'form':SupplierForm(),'data':supplier_table_data,'pageId':page_id})
else:
page_id=str(uuid.uuid4())
request.session["pageid"]=page_id
return render_to_response(urlconfig.index,{'form':SupplierForm(),'data':supplier_table_data,'pageId':page_id})
return render_to_response(urlconfig.index,{'form':SupplierForm(),'data':supplier_table_data,'pageId':page_id}) 有其它思路实现的请多多请教