0

我在 Django 中有一个自定义表单。它工作正常,每当我对其应用 javascript 验证时,验证都不起作用。我想用 javascript 验证它。它显示警报消息,但没有在表单上重定向。

通知:-

<form method="POST" action="#" class="form-horizontal" id="userform" name="uform" enctype="multipart/form-data" >{% csrf_token %}
 <fieldset>

  <div class="control-group formSep">
   <label for="u_id" class="control-label">App Id </label>
     <div class="controls">
      <input type="text" id="appid" class="input-xlarge" name="appid" value="" />
                                    </div>
                                            </div>
    <div class="control-group">
                                                <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>
                                            </div>
</fieldset>
</form>

在views.py中:-

@csrf_exempt
def applicationform(request):
     if request.method == 'POST':

        getappid = request.POST['appid']
        getjobtitle=request.POST['jobtitle']
        odeskid=request.POST['odeskid']
        clientspent=request.POST['client_spent']
        jobtype=request.POST['jobtype']
        notestype=request.POST['notes']
            request.session['setid'] = request.POST['appid']
                if getappid == '':
            return HttpResponse('<script> alert("fill app id"); </script>')
                else:
            getintable = application(app_id = request.POST['appid'], job_title = request.POST['jobtitle'], odesk_id = request.POST['odeskid'],client_spent = request.POST['client_spent'], job_type = request.POST['jobtype'],notes_type = request.POST['notes'])
        getintable.save()
        return HttpResponseRedirect('/applicationview/')        
     else:
        return render_to_response('applicationform.html')
4

3 回答 3

1

在警报语句中执行脚本后重定向

 @csrf_exempt
    def applicationform(request):
         if request.method == 'POST':

            getappid = request.POST['appid']
            getjobtitle=request.POST['jobtitle']
            odeskid=request.POST['odeskid']
            clientspent=request.POST['client_spent']
            jobtype=request.POST['jobtype']
            notestype=request.POST['notes']
                request.session['setid'] = request.POST['appid']
                    if getappid == '':
                return HttpResponse('<script> alert("fill app id"); document.location.href="redirect url" </script>') #change here
                    else:
                getintable = application(app_id = request.POST['appid'], job_title = request.POST['jobtitle'], odesk_id = request.POST['odeskid'],client_spent = request.POST['client_spent'], job_type = request.POST['jobtype'],notes_type = request.POST['notes'])
            getintable.save()
            return HttpResponseRedirect('/applicationview/')        
         else:
            return render_to_response('applicationform.html')
于 2013-09-03T07:41:53.970 回答
0

表单操作需要 URL

<form method="POST" action="/applicationview/" class="form-horizontal" id="userform" name="uform" enctype="multipart/form-data" >
于 2013-09-03T06:44:08.893 回答
0

使用返回假;

  $('#userform').submit(function(){
 var textVal = $("#appid").val();
 if(textVal == "") {
 alert('Text Field Cannot be Empty');
  return false;
  })

如果你想留在同一页面使用return false
或者
如果你想重定向到特定页面使用update_content_div('url');

于 2013-09-03T07:17:55.627 回答