user3030969
user3030969

Reputation: 1575

How to automatically fetch new data from server?

I'm a newbie and I'm making a web app in django, and in it I have a page that shows a list of task objects.

I would like to know how to update this list of objects without refreshing the page, so that if a new object gets created it automatically shows up on the page.

I have some understanding of ajax but, I'm not sure how to achieve this.

TIA

Upvotes: 2

Views: 4748

Answers (1)

Anentropic
Anentropic

Reputation: 33843

You are describing a 'push' of data from the server to the client (web browser)

Django is primarily designed for responding to requests, i.e. the client asks to 'pull' some data from the server.

You can do the pull via Ajax following common tutorials.

You can mimic the 'push' via Ajax by taking a 'polling' approach, where the client periodically asks the server for updates, i.e using setInterval to repeatedly make an Ajax call.

To do true push with Django you're going to have to explore other technologies besides Ajax and Django will need some help/hacks to use them, will have to run on very specific web server platform etc.

If you are building your first webapp in Django I would recommend you avoid trying to do push updates at first and just concentrate on building the app and learning Django.

But to answer the question, options for push data are:

  • HTML5 Server-sent Events
  • HTTP "Long Polling"
  • Web Sockets

See these questions for some further info relevant to using these in Django:
How to build a push system in django?
WebSockets vs. Server-Sent events/EventSource
Does Django have a way to open a HTTP long poll connection?

Some helpers for Django:
https://github.com/niwibe/django-sse
https://github.com/tbarbugli/django_longpolling
https://github.com/stephenmcd/django-socketio

Upvotes: 8

Related Questions