Skip to content

Instantly share code, notes, and snippets.

@prajaybasu
Forked from pradeepmurugan/index.html
Last active December 2, 2017 05:19
Show Gist options
  • Save prajaybasu/136fae3e5dcbff7e8d2dec70e76c19ca to your computer and use it in GitHub Desktop.
Save prajaybasu/136fae3e5dcbff7e8d2dec70e76c19ca 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 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
json_data = json.loads(request.body)
#adding the values in a context variable
context = {
name = json_data["name"]
email = json_data["email"]
phone = json_data["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