Reputation: 139
I have a custom form in django. it's working properly,whenever i apply javascript validation on it than validations not working. I want to validate it with javascript.It show the alert message but did not redirect on form.
In form:-
<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>
In 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')
Upvotes: 0
Views: 100
Reputation: 610
Redirect after your script is executed in the alert statement
@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')
Upvotes: 1
Reputation: 50
Use return False;
$('#userform').submit(function(){
var textVal = $("#appid").val();
if(textVal == "") {
alert('Text Field Cannot be Empty');
return false;
})
if u want lo stay in same page use return false
or
if u want to redirect to specific page use update_content_div('url');
Upvotes: 0
Reputation: 2836
form action requires the URL
<form method="POST" action="/applicationview/" class="form-horizontal" id="userform" name="uform" enctype="multipart/form-data" >
Upvotes: 0