edit
When I write print request.POST['video'], nothing gets printed in the console even through there is a value for 'video'. Am I incorrectly getting the wrong value in the javascript? I am trying to get the 'video34' (the value in the hidden field) to show up.
original
I am trying to POST data using jQuery/AJAX in Django and am having trouble. How do I access the 'video' variable in the views.py? When I write 'print video' in views.py, I get an error in the console saying POST /edit_favorites/ HTTP/1.1" 500 10113.
views.py
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def edit_favorites(request):
if request.is_ajax():
message = "Yes, AJAX!"
else:
message = "Not Ajax"
return HttpResponse(message)
urlconf:
url(r'^edit_favorites/', 'edit_favorites'),
html:
<form method='post' id ='test'>
<input type="hidden" value="video34" />
<input type='submit' value='Test button'/>
<div id = 'message'>Initial text</div>
</form>
javascript:
<script type="text/javascript">
$(document).ready(function() {
$("#test").submit(function(event){
event.preventDefault();
$.ajax({
type:"POST",
url:"/edit_favorites/",
data: {
'video': $('#test').val() // from form
},
success: function(){
$('#message').html("<h2>Contact Form Submitted!</h2>")
}
});
return false;
});
});
</script>