admin管理员组

文章数量:1316980

As described in how to update a Django page without a page reload?, I send periodic XMLHTTPRequests from the browser to the server using JavaScript to get those pieces of the webpage that changes during the course of my application.

However, most of the time, nothing changes; the server replies with the same response and the browser updates the webpage with contents that's already been there.

Obviously, the server should only reply if there is new data.

A thorough web research came up with , but dcoumentation is terse and I'm struggling to implement the concept in my Django project. I have a hard time believing this has not been done before.

Does anyone know any further resources I could use as guideline ?

As described in how to update a Django page without a page reload?, I send periodic XMLHTTPRequests from the browser to the server using JavaScript to get those pieces of the webpage that changes during the course of my application.

However, most of the time, nothing changes; the server replies with the same response and the browser updates the webpage with contents that's already been there.

Obviously, the server should only reply if there is new data.

A thorough web research came up with http://ajaxpatterns/Periodic_Refresh#Lace, but dcoumentation is terse and I'm struggling to implement the concept in my Django project. I have a hard time believing this has not been done before.

Does anyone know any further resources I could use as guideline ?

Share Improve this question edited May 23, 2017 at 10:30 CommunityBot 11 silver badge asked Dec 9, 2009 at 20:24 sscssc 9,91310 gold badges70 silver badges99 bronze badges 1
  • 1 The page you linked to has a good technique: hash the contents, include the hash in the response, send the hash to the server on a timer, and have the server check the hash to know whether to send more stuff. What part do you need help with? – Ned Batchelder Commented Dec 9, 2009 at 20:40
Add a ment  | 

1 Answer 1

Reset to default 9

The difficulty in answering is in not knowing what the server-side resources are that are being returned to the user.

I'll make up something which may serve as an example. Let's say you were developing an application that allowed you to monitor in real-time ments being made by users on your site. We can do several things to make this possible:

  1. The server keeps track of when ments were added (.created field)
  2. The API to get the latest ments requires us to specify how old of ments we want
  3. The view queries and returns only those that have been added since then

models.py

class Comment(models.Model):
    text = models.TextField()
    created = models.DateTimeField(default=datetime.now())

urls.py

url(r'^ments/latest/(?P<seconds_old>\d+)/$',get_latest_ments),

views.py

def get_latest_ments(request, seconds_old):
    """
    Returns ments that have been created since the last given number of seconds
    have elapsed.
    """

    # Query ments since the past X seconds
    ments_since = datetime.datetime.now() - datetime.timedelta(seconds=seconds_old)
    ments = Comments.objects.filter(created__gte=ments_since)

    # Return serialized data or whatever you're doing with it
    return HttpResponse(simplejson.dumps(ments),mimetype='application/json')

On the client-side you get the JSON, check if it has a value, if so enumerate the items, and add the new items to your <div> tag or whatever.


As you can see, the development of the API to return only recently updated items is going to vary based on what content the server is returning.

From your question it sounds like you want the server to manage identifying what is recently updated, not the client (which is a good strategy). In that case, what you need to do is define:

  1. How is the server going to keep track of changes (in my example that's done by the 'created' field)
  2. How is the client going to request those changes
  3. How is the server going to identify which changes have happened in order to return them to the client via API?

本文标签: javascripthow to autoupdate a Django page only when requiredStack Overflow