Reputation:
I am trying to simply update a boolean form value by using an ajax function to update it, because why would I want it to reload, but anyways, I have checked that I am passing the csrf_token, and made sure that's not a problem. I was thinking it was a problem with my urls.py, but I'm not sure exactly what it is.
What can I do to fix this error?
heres my views.py for the ajax form, note: project_complete is a booleanfield in my model
@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
<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'),
)
Upvotes: 2
Views: 7557
Reputation: 9549
Just a general tip as you have posted all the code including HTML / JS / Python / urls.py ...
In general read the most frequent response codes and what they mean here HTTP/1.1: Status Code Definitions
Upvotes: 11