views:

453

answers:

2

I have two sites written with Django. What I want is to load a content from one site into another. I found out that best choice to do this would be using .getJSON and JSON-P but I have no idea how to put this things (Django, jQuery and JSONP) together.

Any help from more expirienced users?

EDIT

I'm trying to achive an ongoing process with things being loaded from one site into another on every pageview - not a single dump/loaddata stuff.

A: 

The best solution is to put both sites in the same database. Then site2 can simply read site1's database. Indeed, the site2 can include the site1 application programs, making site2 include all of the site1 features.

If, for some reason, you can't get site1 and site2 to share a common database, then site2 must get data from site1 by requesting it through HTTP.

In the site2 application, you use the same urls.py as site1. The site2 view functions, however, use urllib2 to make HTTP GET and POST requests to site1. Once the site2 view function has the response from site1, it simply returns it.

Your site2 applications can look something like this.

def someSite2View( request ):
    site1= urllib2.open( "http://site1/" + request.path  )
    data= site1.read()
    # you have to parse the data to extract the headers
    return Response( headers and data )

This is suprisingly workable. This sort of forwarding happens a lot in HTTP pipelines.

S.Lott
I think you misunderstood me a little bit. I want to load "live" content since data provied by view in project #1 is changing, and i need it to be also displayed in the project #2. Not a single operation -- rather more ongoing process
zxc
+1  A: 

To expose your JSONP endpoint use django-rest-interface.

To load it from the other site use Jquery's getJSON.

Paul Tarjan