私は名前、年齢、電子メールのフィールドを持つ学生モデルを持っています.私はこのためにStudentFormというフォームを作成し、このようなビューを作成しました
def student(request):
form=Studentform(request.POST)
if request.method=='POST':
if form.is_valid():
stu=Student()
stu.name=form.cleaned_data['name']
stu.email=form.cleaned_data['email']
stu.age=form.cleaned_data['age']
stu.save()
return HttpResponseRedirect('/index/')
else:
form=Studentform()
return render_to_response('index.html',{'form':form},context_instance=RequestContext(request) )
そしてここに私のindex.html
<html>
<head>
<title></title>
<script type="text/javascript">
var student={{ stu_var }}
alert("erer")
</script>
</head>
<body>
<form action="/index/" method="post">{% csrf_token %}
{{form.as_p}}
<input type="submit" id="submit" name="submit" onclick="alert(student)">
</form>
</body>
</html>
ここで、学生ビューで json 応答を作成して、学生オブジェクトのすべての値を保持し、投稿時にそれを index.html にレンダリングして、次のようなアラートを生成できるようにします--->「Aditya SIngh、Youデータの送信に成功しました。」ここで、Aditya Singh は django.thanx の新しい生徒の名前になります。