如何具体检查是否单击了表单的提交按钮?
if form.is_valid()
验证会执行该检查,但还会返回True,而无需单击按钮(当执行clean()方法时)。单击提交按钮后,如何将变量设置为True。我尝试确认上传,并且当用户单击“提交”按钮时,它应该执行数据库查询。
这是我的views.py
def file_upload(request):
save = False
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
form2 = ConfirmationForm(request.POST, request.FILES)
if cache.get('save'):
print("True")
save = True
else:
print("False")
save = False
print("request.POST: ", request.POST)
print("request.FILES: ", request.FILES)
print("save: ", save)
print("form: ", form)
print("form2: ", form2)
if not request.FILES:
print("empty")
else:
print("not empty")
updated_obj = 0
created_obj = 0
duplicate_obj = 0
count = 0
# Prüfen ob CSV Datei valide ist (keine doppelten Datensätze)
rs1 = list()
rs2 = list()
d_list1 = list()
d_list2 = list()
csv_file = get_data(request, save)
csv_file.seek(0)
file = csv_file.read().decode('utf-8').splitlines()
reader = csv.reader(file)
for row in reader:
try:
row_set1 = [row[0], row[1], row[2], row[3], row[4]]
row_set2 = [row[5], row[6]]
if row_set1 not in rs1:
rs1.append(row_set1)
else:
d_list1.append(row_set1)
if row_set2 not in rs2:
rs2.append(row_set2)
else:
d_list2.append(row_set2)
except IndexError:
break
if len(d_list1) and len(d_list2):
messages.error(request, "In der CSV Datei sind folgende Datensätze mehrfach vorhanden")
messages.error(request, "Dosen: %s " % d_list1)
messages.error(request, "Ports: %s " % d_list2)
return redirect('appp:index')
elif len(d_list1) and not len(d_list2):
messages.error(request, "In der ausgewählten CSV Datei sind folgende Dosen mehrfach vorhanden")
messages.error(request, "%s " % d_list1)
return redirect('appp:index')
elif not len(d_list1) and len(d_list2):
messages.error(request, "In der ausgewählten CSV Datei sind folgende Ports mehrfach vorhanden")
messages.error(request, "%s " % d_list2)
return redirect('appp:index')
# Wenn CSV Datei valide ist --> Datei hochladen
updated_list = list()
created_list = list()
csv_file.seek(0)
file = csv_file.read().decode('utf-8').splitlines()
reader = csv.reader(file)
print("reader: ", reader)
for row in reader:
count += 1
try:
datet = datetime.now().date()
datum = datet.strftime("%d.%m.%Y")
row[7] = datum
row[8] = str(request.user)
dataset1 = CSV5.objects.filter(gebaeudebereich=row[0],
gebaeudenummer=row[1], ebene=row[2],
raum=row[3], dose=row[4])#.values_list("gebaeudebereich", "gebaeudenummer",
# "ebene", "raum", "dose")
dataset2 = CSV5.objects.filter(switch_ip=row[5], switch_port=row[6])#.values_list("switch_ip", "switch_port")
#ds1 = CSV5.objects.values_list("gebaeudebereich", "gebaeudenummer", "ebene",
#"raum", "dose")
#ds2 = CSV5.objects.values_list("switch_ip", "switch_port")
#print("ds1: ", ds1)
print("count: ", count)
print("dataset1: ", dataset1)
print("dataset2: ", dataset2)
print("dataset1: ", dataset1.exists())
print("dataset2: ", dataset2.exists())
print("save: ", save)
if (dataset1.exists() and not dataset2.exists()):
#instance = get_object_or_404(CSV5, id=count)
print("Fall 1")
ins = CSV5.objects.filter(gebaeudebereich=row[0],
gebaeudenummer=row[1], ebene=row[2],
raum=row[3], dose=row[4])
#liste.append(ins.values_list())
#print("liste: ", liste)
print ("instance: ", ins)
if save == True and form2.is_valid():
ins.update(switch_ip=row[5], switch_port=row[6], datum = row[7], akteur = row[8])
else:
updated_list.append(ins)
updated_obj += 1
elif not dataset1.exists() and dataset2.exists():
print("Fall 2")
ins = CSV5.objects.filter(switch_ip=row[5], switch_port=row[6])
print("instance: ", ins)
if save == True and form2.is_valid():
ins.update(gebaeudebereich = row[0], gebaeudenummer = row[1], ebene = row[2], raum = row[3],
dose = row[4], datum = row[7], akteur = row[8])
else:
updated_list.append(ins.values_list())
print("dat2: ", dataset2)
updated_obj += 1
elif (dataset1.exists() and dataset2.exists()):
print("Fall 3")
#liste.append(dataset1)
#print("liste: ", liste)
duplicate_obj += 1
elif not (dataset1.exists() and dataset2.exists()):
print("Fall 4")
if save == True and form2.is_valid():
ins = CSV5.objects.get_or_create(id=CSV5.objects.count() + 1, gebaeudebereich=row[0],
gebaeudenummer=row[1], ebene=row[2],
raum=row[3], dose=row[4], switch_ip=row[5], switch_port=row[6],
datum=row[7], akteur=row[8])
created_list.append(ins)
ins.save()
created_obj += 1
except IndexError:
print("IndexError")
break
except IntegrityError:
duplicate_obj += 1
print("IntegrityError")
if save == False:
messages.success(request, "Einträge die neu hinzugefügt werden: %s " % created_obj)
messages.success(request, "Einträge die überschrieben werden: %s " % updated_obj)
messages.success(request, "Duplikate: %s " % duplicate_obj)
elif save == True:
messages.success(request, "Objects created: %s" % created_obj)
messages.success(request, "Objects updated: %s " % updated_obj)
messages.success(request, "Duplicate Objects: %s " % duplicate_obj)
return redirect('appp:index')
form2 = ConfirmationForm()
return render(request, "appp/confirmation.html", {'form' : form2})
form = UploadFileForm()
return render(
request, "appp/file_upload.html", {"form": form}
)
这是一个缓存文件的辅助功能:
def get_data(request,save):
data = cache.get('cached_file')
if save:
data = cache.get('cached_file')
cache.set('save', False,timeout=1200)
else:
data = request.FILES['file']
print("dt: ", data)
cache.set('cached_file', data, 1200)
cache.set('save',True, 1200)
return data
任何帮助将不胜感激。预先感谢
答案 0 :(得分:1)
在模板html文件上
<form method='get' action='#'>
<input type="submit" value="YourValue" name="NameOfYourButton"/>
</form>
在您的views.py文件中
if request.GET.get('NameOfYourButton') == 'YourValue':
print('user clicked button')