English 中文(简体)
使用Django的jquery-ajax功能问题
原标题:Question on a jquery-ajax function, Using Django

我是新到Django、Ajax和超文本的。 我目前正在为学习目的开发一个简单的网站。

该网站提供我的剪辑最近的消息,显示最近5个新闻频道。 如果用户不更新整页,就应当这样做。

下面的法典似乎并不多,但这是我结束以来令人痛苦的长期学习进程的结果。

Question comes at the end.

url.py

site_media =  /django/feed/site_media/ 

urlpatterns = patterns(  ,
(r ^$ ,  recent.views.recent_feed_view ),
# Site media
(r ^site_media/(?P<path>.*)$ ,  django.views.static.serve ,
{ document_root : site_media}),)

models.py

class news(models.Model):
   question = models.CharField(max_length=200)
   pub_date = models.DateField( date published )

views.py

def recent_feed_view(request):
show_results = False
new_feed = []
if  get_feed  in request.GET:
    show_results = True
    new_feed = news.objects.order_by( -pub_date )[:5]

variables = RequestContext(request, {
     new_feed : new_feed,
     show_results : show_results,})

if request.GET.has_key( ajax ):
    return render_to_response( feed_list.html , variables)
else:
    return render_to_response( recent_news.html , variables)

recent_news.html

<html>
<head>
    <script type="text/javascript" src="/site_media/jquery.js"> </script>
    <script type="text/javascript"src="/site_media/get_feed.js"></script>
</head>

 <body>
    <form id="search_feed_form" method="get">
        <input name = "get_feed" type="submit" value="get_feed" />
    </form>

  <div id="get_feed_div">
      {% if show_results %}
          {% include "feed_list.html" %}
      {% endif %}
  </div>
</body>
</html>

feed_list.html>

    <ol class="news">
    {% for news in new_feed %}
        {{ news.question }}
        <br />
    {% endfor %}
</ol>

get_feed.js

function get_feed_js() {
$( #get_feed_div ).load( /?ajax&get_feed );
return false;}

$(document).ready(function () {
    $( #search_feed_form ).submit(get_feed_js);});

< Questions:

1- How can I modify my javascript function so that the page automatically fetches the latest news feed, say every 1 min, without having to press the button?

2- 在用户浏览我网站时,是否有办法收集最新新闻资料? (i.e:他目前正在浏览他的浏览器上另一个表格,然后在表格上点击。)

我非常赞赏我能收到一些可帮助我推进这一努力的有用材料链接。

最佳回答

http://www.eated.com/articles/javascript-timers-with-settimeout-and-setinterval/"rel=“nofollow” http://www.eated.com/articles/javascript-timers-with-setout-and-setinterval/

setInterval( get_feed_js() , 60000); // call func every 60 seconds





$(window).one( focus , function() { // without one, I infinitely get focus event
    get_feed_js(); // call function on tab focus
})
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...