我正在创建私有消息应用程序,并以这样的方式呈现消息:
return render(request,'mssg/mssg_page.html', {'inbox':inbox, 'current_mssg':current_mssg,})收件箱包含所有邮件。当前消息是一条应该充分显示的消息。我在模板中列出了如下内容的消息:
{% for mssg in inbox %}
<a href="#"><!-- here -->
<div class="right">
<h3>{{ mssg.mssg_from.username }} <small>{{ mssg.delivery_date }}</small></h3>
<h2>{{ mssg.title|safe }}</h2>
<p>{{ mssg.text|safe|truncatewords:15 }}</p>
</div>
</a>
{% endfor %}我也在同一个模板中显示完整的当前消息。
单击带有<!-- here -->注释的链接后,我想更改当前消息
我知道我可以发送消息id来查看、验证它并将其发送回模板。但是它需要更新页面。但是,所有消息都已在inbox中列出的模板中,因此不需要刷新页面。我也知道可以从收件箱中选择像{{inbox.0}}这样的信息
但我不知道如何使它依赖于点击链接。我想它需要javascript。但是,即使我知道我可以通过在模板中放置inbox[0]来做类似inbox.0的事情,我仍然不知道如何做类似于inbox[varriable]的事情。
有办法吗?
如果我的页面不需要刷新,我不希望它刷新。
我用的是引导
发布于 2016-07-29 15:54:14
使用AJAX将解决您的问题。参见this StackOverflow answer,它解释了如何做到这一点:
$('a').click(function() { // This is a listener to the link you mentioned.
$.ajax({
url: '127.0.0.1:8000/your_ajax_url', // You will need to create a URL for your AJAX call.
type: 'get', // This is the default though, you don't actually need to always mention it
success: function(data) {
alert(data);
},
failure: function(data) {
alert('Got an error dude');
}
});
}https://stackoverflow.com/questions/38659544
复制相似问题