Reputation: 119
I have written a model named tablestoreajax. It contains name and age fields. I want to store this table field values using ajax but without using json. Am new to django. models.py
class tablestoreajax(models.Model):
name=models.CharField(max_length=20)
age=models.IntegerField(default=0)
tablestore.html
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#form1').on('submit',function(){
var name=$('#id1').val()
var age=$('#id2').val()
var dataString = {name:name,age:age};
$.ajax({
type:'GET',
data:dataString,
url:'/ajaxdisplay/',
success:function(data) {
alert(data);
}
});
});
});
</script>
</head>
<body>
name<input type="text" id="id1" name="name1">
age<input type="text" id="id2" name="age1">
<input type ="submit" id="sub" value="save">
</body>
</html>
I don't know how to write my views with get method. How to proceed? I need your help
Upvotes: 0
Views: 689
Reputation: 3089
Simply define a url and view just as you would regularly.
from django.conf.urls import patterns, include, url
urlpatterns = patterns('myproject.app.views',
url(r'^ajaxdisplay/$', 'ajaxView'),
)
def ajaxView(request):
#...
If you want, you can use json to return variables you may want to return to your client.
from django.utils import simplejson
def ajaxView(request):
data = {
'name':request.GET['name'],
'age':request.GET['age'],
}
return HttpResponse(simplejson.dumps(data))
$.ajax({
type:'GET',
data:dataString,
url:'/ajaxdisplay/',
datatype:'json', //don't forget to specify datatype
success:function(data) {
alert(data.name);
alert(data.age);
}
});
Upvotes: 1