Skip to content

Instantly share code, notes, and snippets.

@pradeepmurugan
Created December 2, 2017 05:10
Show Gist options
  • Save pradeepmurugan/550e15c4c243a48c01334d43b10b5cf7 to your computer and use it in GitHub Desktop.
Save pradeepmurugan/550e15c4c243a48c01334d43b10b5cf7 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Forms Tutorial</title>
</head>
<body>
<h2>Django Forms Tutorial</h2>
<form method="POST" action="/getdata/" name="myForm">
<table>
<tr>
<td>Enter your name:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Enter your email:</td>
<td><input type="email" name="email"/></td>
</tr>
<tr>
<td>Enter your phone:</td>
<td><input type="text" name="phone"/></td>
</tr>
<tr>
<td>
</td>
<td>
<input value="Submit" type="submit" onclick="submitForm()">
</td>
</tr>
</table>
</form>
<script>
function submitform(){
var formData=JSON.stringify($("myFOrm").serializeArray());
$.ajax({
type: "POST",
url: "serverUrl",
data: formData,
success: function(){},
dataType: "json",
contentType : "application/json"
});
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Forms Tutorial</title>
</head>
<body>
<h2>Django Forms Tutorial</h2>
<form method="post" action="/getdata/">
<table>
<tr>
<td>Your Name</td>
<td><strong>{{ name }}</strong></td>
</tr>
<tr>
<td>Your Email</td>
<td><strong>{{ email }}</strong></td>
</tr>
<tr>
<td>Your Phone</td>
<td><strong>{{ phone }}</strong></td>
</tr>
</table>
</form>
</body>
</html>
#importing required packages
from django.http import HttpResponse
from django.template import loader
from django.views.decorators.csrf import csrf_exempt
#disabling csrf (cross site request forgery)
@csrf_exempt
def index(request):
#if post request came
if request.method == 'POST':
#getting values from post
name = request.POST.get('name')
email = request.POST.get('email')
phone = request.POST.get('phone')
#adding the values in a context variable
context = {
received_json_data['name']=json.loads(request.body['name'],
received_json_data['email']=json.loads(request.body['email'],
received_json_data['phone']=json.loads(request.body['phone']
}
#getting our showdata template
template = loader.get_template('showdata.html')
#returing the template
return HttpResponse(template.render(context, request))
else:
#if post request is not true
#returing the form template
template = loader.get_template('index.html')
return HttpResponse(template.render())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment