我有一个表单,它成功地将数据发送到我的视图,然后返回一个JSON对象,我有一个包含AJAX的相应模板,在将数据提交到服务器后调用它,但是当打印出数据时,它似乎是只是模板HTML代码,而不是发送的JSON,我在做什么,显然是错的?
<body>
<form action="." method="post" id="myform">{% csrf_token %}
{{ myform.as_p }}
<p>Date: <input type="text" name="start_date" id="start_date"></p>
<p>Date: <input type="text" name="end_date" id="end_date"></p>
<p>Date: <input type="text" name="my_form" id="my_form"></p>
<input type="submit" name="submit" value="SUBMIT" id="submit" />
</form>
</body>
对应的ajax
$.ajax({
url: 'http://127.0.0.1:8000/mysite',
data: {
format: 'json'
},
error: function() {
$('#info').html('<p>An error has occurred</p>');
},
success: function(data) {
alert( data)
},
type: 'get'
})
视图:
def index(request):
if request.method == 'POST':
post = request.POST
my_form = GraphForm(request.POST)
if my_form.is_valid():
response = {
'status':str(my_form.cleaned_data['my_form']), }
obj = {"a": "SHELL", "b": "ghost"}
return render(request,"mysite/index.html", obj,content_type="application/json" )
else:
form = GraphForm()
return render(request,"mysite/index.html", { 'form' : form } )
else:
form = GraphForm()
return render(request,"mysite/index.html", { 'form' : form } )