如何刷新django模板中的某个元素?
例:
{% if object.some_m2m_field.all %}
<h3>The stuff I want to refresh is below</h3>
<div id="the-div-that-should-be-refreshed">
{% for other_object in object.some_m2m_field.all %}
<a href="www.example.com">{{ other_object.title }}</a>
{% endfor %}
</div>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
让我们说页面中的其他一些元素会触发一个应该刷新上面div的javascript.有没有办法让django刷新模板中的这个特定元素?
如果没有,我将不得不使用常规JS或jQuery方法对div进行修补,而不是使用django模板层的强大功能.另外,上面的代码是对实际模板的简化,我使用了很多模板的功能,因此猴子修补生成的html将是一场噩梦......
我是django的新手,无法找到只刷新div的方法,div显示我目前的星级评分.我的想法是,用户可以通过点击星标来查看平均评分和评分,点击后我希望星标显示新的平均评分而不刷新整个页面.
这是div:
<div id="rating">
<div class="movierating" id="o_{{ object.id }}">
<span style="display:none;">{{ object.rating_vote.get_rating }}</span>
{{ object.rating_vote.get_rating }}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
而javascript是:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('.movierating').each(function(index){
$(this).raty({
readOnly: false,
path: "{{ STATIC_URL }}images/",
start: $(this).children("span:first").text(),
click: function(score, evt) {
var vote_url = "/rate/" + this.attr('id').substring(2) + "/" + score + "/";
var div = this.attr('id');
$.ajax({
url: vote_url,
success: function(){
alert('Vote successful!);
$('#rating').load('#rating');
},
});
}
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
有了这段代码
$('#rating').load('#rating');
我将整个页面放入div中,当我使用时
$('#rating').load('# rating');
没有星星,我只获得价值.