var send_data = { 'name': place.name, 'address': address};
var csrftoken = $.cookie('csrftoken');
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
crossDomain: false, // obviates need for sameOrigin test
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
}
}
});
$.ajax({ url: '/results/',
type: 'POST',
data: send_data,
success: function(response, status, jqXHR) {
$("#results").html(response);
//console.log('success function resp');
//console.log(jqXHR.getAllResponseHeaders());
},
error: function(obj, status, err) { alert(err); console.log(err); }
});
控制注入模板和正常页面查看的示例:
{% extends x|yesno:"stamped/blank.html,stamped/home.html" %}
{% load stamped_custom_tags %}
{% block results %}
<! Your HTML Here >
<h1> title and stuff </h1>
<div> I contain things! </div>
{% endblock %}
home.html是我的index.html,它包含#resultsdiv!你知道吗
Blank.html:
{% block results %}{% endblock %}
<! to allow for corrected shared rendering
with ajax posts and normal django rendering >
我不知道网上有什么例子能说明这一点,但我有一个包含这一功能的个人项目;agconti:stamped。你知道吗
如果您想克隆并测试整个repo,那么上面会链接整个repo,但是与您的需求相关的代码是
Ajax帖子:
控制注入模板和正常页面查看的示例:
home.html
是我的index.html
,它包含#results
div!你知道吗Blank.html
:任何呈现页面的普通django视图都将返回html,上面的ajax post将注入ajax post发起的页面上的
#results
div(不包括在上面的示例中)。你知道吗编辑您的评论:
我假设你真正想在预览中显示的是一些重要的东西,比如一篇文章的标题。我会有一个单独的预览,呈现预览模板视图。在该模板中,我可以执行以下操作:
然后,您可以将其注入到页面中,并在例如鼠标上显示它。你知道吗
相关问题 更多 >
编程相关推荐