问题描述
我正在尝试为django应用程序创建通知,在该应用程序中必须在模板的div中显示数据库中的更改.我希望ajax定期访问我的数据库,如果有任何更改,则应将其显示在该div中.我希望我的数据来自3个不同的模型,并且任何一个模型中的任何更改都应在模板的div中进行通知.
I am trying to create notifications for my django app where I have to display changes in database in a div in my template. I want ajax to hit my database periodically and if there are any changes then they should be displayed in that div.I want my data to come from 3 different models and any change in either of them should be notified in my template's div.
我该怎么做?
模板:
<div class="notification">
{% for like in likes_notify %}
{% if like.item.reward != None %}
<div class="like_newsfeed">
<a href="/employee/{{ emp_code }}/social-info/single-news-feed/{{ like.item.reward.pk }}/show/"> {{ like.user.get_full_name }} liked your post : {{ like.item.reward }} </a>
</div>
{% else %}
<div class="like_news_post">{{ like.item.comment.pk }}
<a href="/employee/{{ emp_code }}/social-info/single-news-feed/{{ like.item.comment.pk }}/show/"> {{ like.user.get_full_name }} liked your comment "{{ like.item.comment }}". </a>
</div>
{% endif %}
{% endfor %}
<br><hr>
{% for comment in comments_notify %}
{% if comment.item.reward != None %}
<div class="comment_newsfeed">
<a href="/employee/{{ emp_code }}/social-info/single-news-feed/{{ comment.item.reward.pk }}/show/"> {{ comment.user.get_full_name }} commented on your post : {{ comment.item.reward }} with "{{ comment }}". </a>
</div>
{% else %}
<div class="comment_news_post">{{ comment }}
<a href="/employee/{{ emp_code }}/social-info/single-news-feed/{{ comment.item.reward.pk }}/show/"> {{ comment.user.get_full_name }} commented on your post "{{ comment }}". </a>
</div>
{% endif %}
{% endfor %}
<br><hr>
{% for reward in rewards_notify %}
<div class="reward_newsfeed">
<a href="/employee/{{ emp_code }}/social-info/single-news-feed/{{ reward.pk }}/show/"> {{reward.role_history.organisation_role.user.get_full_name }} got a reward : {{ reward }} </a>
</div>
{% endfor %}
</div>
JS:
function worker(){
var url = '/employee/'+get_emp_code()+'/home/dashboard/';
// $(".notification").load(url + ' .notification',data, function () { setTimeout(worker, 5000); });
$.ajax({
type:'get',
url: url,
success: function(data) {
// $('.notification').load(url + " .notification", data);
// $('.notification').load(url + " .notification").html(data);
$('.notification').html(data);
},
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(worker, 3000);
}
});
}
$(document).ready(function(){
setTimeout(worker, 5000);
});
我尝试编写此js,但这会两次加载整个页面,而不是div.实际上,我已经将基本模板扩展到该模板中,因此它也将整个基本模板的数据加载到我的div中.
I have tried writing this js but this loads my entire page twice and not the div. I have actually extended a base template into this template so it loads the entire base template's data also in my div.
查看:
def get_context(request,*args,**kwargs):
context = {}
likes = Like.objects.exclude(user = request.user).order_by('-date_edited')[:2]
comments = Comment.objects.filter(user = request.user).order_by('-last_updated')[:2]
rewards = Reward.objects.filter(role_history__organisation_role__organisation=request.user.organisation_user.organisation, approved=True).order_by('-last_updated')[:2]
#notification = chain(likes,comments,rewards)
context.update({'likes_notify':likes,'comments_notify':comments,
'rewards_notify':rewards})
这是我要为其发送通知的部分.
This is the portion in my view for which i want to make my notifications for.
推荐答案
最终找到了我的问题的答案.我刚刚用以下代码修改了我的js:
Finally found the answer to my question. I just modified my js with this code:
JS:
success: function(data) {
var dtr = $('.notification',data);
$('.notification').html(dtr);
}
这篇关于使用jquery,ajax和django定期刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!