【问题标题】:Refresh page periodically using jquery, ajax and django使用 jquery、ajax 和 django 定期刷新页面
【发布时间】:2014-05-19 08:27:12
【问题描述】:

我正在尝试为我的 django 应用程序创建通知,我必须在模板中的 div 中显示数据库中的更改。我希望 ajax 定期访问我的数据库,如果有任何更改,那么它们应该显示在该 div 中。 我希望我的数据来自 3 个不同的模型,其中任何一个模型的任何更改都应在模板的 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/"> &nbsp;{{ like.user.get_full_name }} liked your post : {{ like.item.reward }}&nbsp;&nbsp;&nbsp; </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/">&nbsp;{{ like.user.get_full_name }} liked your comment "{{ like.item.comment }}".&nbsp;&nbsp;&nbsp; </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/"> &nbsp;{{ comment.user.get_full_name }} commented on your post : {{ comment.item.reward }} with "{{ comment }}".&nbsp;&nbsp;&nbsp; </a>
            </div>
        {% else %}
            <div class="comment_news_post">{{ comment }}
                <a href="/employee/{{ emp_code }}/social-info/single-news-feed/{{ comment.item.reward.pk }}/show/"> &nbsp;{{ comment.user.get_full_name }} commented on your post "{{ comment }}".&nbsp;&nbsp; </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/">&nbsp;{{reward.role_history.organisation_role.user.get_full_name }} got a reward :  {{ reward }}&nbsp;&nbsp;&nbsp;</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 中加载了整个基本模板的数据。

查看:

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})

这是我认为我想要通知的部分。

【问题讨论】:

  • 您已经尝试过什么了吗?发布您现有的代码并描述您在方法中遇到的具体问题,人们会帮助您解决问题。
  • 请检查我编辑的代码并尽可能提供帮助。现在我只是显示用户名。一旦我在模板中正确显示通知,我将对其进行修改。

标签: jquery ajax django django-templates django-views


【解决方案1】:

终于找到了我的问题的答案。我刚刚用这段代码修改了我的 js:

JS:

success: function(data) {
var dtr = $('.notification',data);
$('.notification').html(dtr);
}

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多