I get some form values by ajax and post them to preview page. but I couldn’t get how to reach those values in my template file. here are my codes:
my ajax function:
$('#preview').click(function() {
var title = $('#id_title').val(); // form input1
var body = $('#id_body').val(); // forminput2
var ajaxOptions = {
type:'post',
url : '/admin/post/preview/',//preview page's url which defined in urls.py
data : {
'title_preview' : title,
'body_preview' : body
},
success: function(){
window.open("/admin/post/preview"); // to see preview of post in a new window
},
error: function(){
alert('olmadı');
}
};
$.ajax(ajaxOptions);
my view:
@require_POST
def preview(request):
title_preview = request.POST.get('title_preview')
body_preview = request.POST.get('body_preview')
query = Post(titlePreview = title_preview,bodyPreview = body_preview,owner = request.user)
query.save()
return render_to_response('preview.html',{'post':query},context_instance=RequestContext(request))
but it doesn’t work. it seems blank page.I don’t know what is wrong.
You’re posting to your preview view, then when that returns successfully you are trying to open a separate window by calling the same URL again. But the second call doesn’t have any POST data, so naturally it won’t work. Your
successfunction should do something with the data it’s already being passed, rather than trying to open a new URL.