Reputation: 17
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.
How can I do that?
Template:
<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);
});
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.
View:
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.
Upvotes: 1
Views: 1239
Reputation: 17
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);
}
Upvotes: 1