我试图通过使用ajax函数更新它来简单地更新一个布尔形式值,因为为什么我要它重新加载,但无论如何,我已经检查过我传递了csrf_token,并确保&#39不是问题。我认为这是我的urls.py的一个问题,但我不确定它究竟是什么。
我该怎么做才能解决此错误?
继承我的views.py for ajax表单,注意:project_complete是我模型中的booleanfield
@login_required
def ProjectDetailToDoCForm(request):
form = ProjectToDoCForm(request.POST or None)
if form.is_valid() and request.is_ajax():
args = {}
args.update(csrf(request))
ajaxVal = request.POST.get('booleanValue')
args['doneBool'] = ajaxVal.project_complete
return HttpResponse(json.dumps(args), content_type="application/json")
的javascript
<script type="text/javascript">
$(document).on("submit", "#project_edit_date", function(e){
e.preventDefault();
updateForm();
});
function updateForm() {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});
$.ajax({
url: "{% url 'projects:todoc_form' %}",
type: "POST",
datatype: "json",
data: {booleanValue : $("#id_project_complete").val()},
"beforeSend": function(xhr, settings) {
console.log("Before Send");
$.ajaxSettings.beforeSend(xhr, settings);
},
success: function(json){
console.log(json);
console.log("success");
},
error:function(xhr,errmsg,err){
console.log(err);
}
});
}
</script>
形式
<form action="" method="post" id="project_edit_date">{% csrf_token %}
<label for="todoc">Task Done?</label>
<span name="todoc" id="check_done"> {{todocform.project_complete}}</span>
<button type="submit" id="project_edit_button">
<span>Update</span>
</button>
</form>
urls.py
urlpatterns = patterns('',
url(r'^$', views.ProjectView.as_view() , name='project'),
url(r'^create/$', views.createproject, name='create'),
url(r'^edit/(?P<slug>[\w-]+)/$', views.ProjectDetail.as_view(), name='indproject'),
url(r'^view/(?P<slug>[\w-]+)/$', views.ProjectDetailPublic.as_view(), name='pproject'),
url(r'^form/(?P<slug>[\w-]+)/$', require_POST(ProjectDetailForm.as_view()), name='indproject_form'),
url(r'^update/(?P<slug>[\w-]+)/$', require_POST(ProjectDetailToDoForm.as_view()), name='todo_form'),
url(r'^complete/$', ProjectDetailToDoCForm, name='todoc_form'),
)
答案 0 :(得分:2)
只是一般性提示,因为您已发布所有代码,包括HTML / JS / Python / urls.py ......
一般情况下,请阅读最常见的回复代码及其含义HTTP/1.1: Status Code Definitions