我有一个表格,我在上面使用了 javascript 验证。在表单上重定向时显示警报后,它会清空所有字段值。以自定义形式:
form method="POST" action="#" class="form-horizontal" name="jform" enctype="multipart/form-data" >{% csrf_token %}
<fieldset>
<div class="control-group formSep">
<label for="u_fname" class="control-label">oDesk Id</label>
<div class="controls">
<input type="text" class="input-xlarge" name="odesk" value="" />
</div>
</div>
<div class="control-group formSep">
<label for="u_fname" class="control-label">Hours</label>
<div class="controls">
<input type="text" class="input-xlarge" name="hours" value="" />
</div>
</div>
<div class="controls">
<button class="btn btn-gebo" type="submit" name="asubmit">Submit</button>
<input type="reset" name="reset" value="Cancel" class="btn btn-gebo" />
</div>
在views.py中:-
@csrf_exempt
def odesk(request):
if request.method == 'POST':
odeskid=request.POST['odesk']
hours = request.POST['hours']
feedback= request.POST['feedback']
if odeskid == '':
return HttpResponse('<script> alert("fill odesk id here "); document.location.href="/odesks/"; </script>')
if hours == '':
return HttpResponse('<script> alert("fill total hours"); document.location.href="/odesks/"; </script>')
if feedback == '':
return HttpResponse('<script> alert("fill feedback "); document.location.href="/odesks/"; </script>')
else:
getintable = odesktable(odeskid = request.POST['odesk'], hours = request.POST['hours'], feedback = request.POST['feedback'])
getintable.save()
return render_to_response('odeskform.html')
else:
return render_to_response('odeskform.html')
它的工作,但在它重定向时发出警报后清除所有字段。