1
這是我的觀點:Ajax,jQuery,Django。這是如何獲得設置?
def planning(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('loginregistration.views.login'))
if request.is_ajax:
POST = request.POST
msg = "Success"
print request.POST
return HttpResponse(msg)
else:
form = planForm()
return render(request, 'plan.html', {'form':form})
這裏是我的html:
{% extends "base.html" %}
{% block title %}Plan{% endblock %}
{% block content %}
<a>Welcome to planning</a>
{{form.as_p}}
<script>
<----- I need to know how to set this up ---->
</script>
{% endblock %}
我明白我需要做一個按鈕,點擊時提交我的形式,而是從例子中我見過的互聯網它不是很直觀。有人可以給我一個例子,我將如何得到這個頁面上的一個按鈕來發送ajax呼叫到服務器。
編輯: 我該如何鉤住這個按鈕?
{% extends "base.html" %}
{% block title %}Plan{% endblock %}
{% block content %}
<a>Welcome to planning</a>
{{form.as_p}}
<button id="form_submit">Submit</button>
<script>
$('button#form_submit').click(function() {
$.ajax({
type: "POST",
url:"/planning/submit/",
data: {
'test': 'success',
},
success: function(){
alert('test')
},
error: function(){
alert("Error");
});
</script>
{% endblock %}
不適用於ajax – user3043594
這是什麼意思? – sundance
request.is_ajax!= request.method =='POST':這不適用於$ .post – user3043594