2012-10-10 55 views
0

如何在我的html/jquery中使用upload_response來檢查它是否爲真,並相應地顯示一些文本而無需轉到新頁面?django ajax上下文

fileupload.py文件:

template = loader.get_template('start_interview.html') 
    context = Context({ 'upload_response': 'True'}) 
    return HttpResponse(template.render(context)) 
+0

你是什麼意思{無需進入新的頁面} –

+0

就像在同一頁內的AJAX評論,而不是去到一個新的URL /新頁 – user1678031

+0

更新了答案。那是你在找什麼? –

回答

0

您將需要使用一些像jQuery的ajax part

$.ajax({ 
    type: "POST", 
    url: "<your url to the view that returns appropriate template>", 
    data: { name: "John", location: "Boston" } 
}).done(function(responseMsg) { 
    $('#notifier').html(responseMsg) 
}); 

responseMsg將呈現模板。有多種方法可以完成上述任務。

在模板

{%if upload_response %} 
...Your html tags or jquery if inside script block... 
{%else%} 
...Do Stuff if it is false... 
{%endif%} 
+0

謝謝 - 非常感謝,我是一個新手!所以{%if upload_response%}標籤可以在腳本標籤中使用? – user1678031

+0

是的,他們做! Django模板引擎不會區分HTML頁面或JavaScript的各個部分,只要它們在html中可見,即模板變量在包含的css/js文件中不起作用。 –

+0

太棒了!它的工作=)謝謝! – user1678031