ondrejsika.com

django-remote documentation

Library for AJAX access to Django from other site (PHP, WordPress, ...)

Authors

Ondrej Sika ondrejsika.com dev@ondrejsika.com

Source

Documentation ondrejsika.com/docs/django-remote
Python Package Index pypi.python.org/pypi/django-remote
GitHub github.com/sikaondrej/django-remote

Instalation

Instalation is very simple over pip.
# pip install django-remote

settins.py

# add app to installed apps
INSTALLED_APPS += ("remote_ajax", )

# XHR allowed domains
ACCESS_CONTROL_ALLOW_ORIGIN = ("http://localhost", "http://my-wordpress-site.com")

# global allow XHR (not requed, you may use remote_ajax.http.HttpResponseAjax)
MIDDLEWARE_CLASSES += ("remote_ajax.middleware.XHRMiddleware", ) 

HttpResponseAjax

your views.py

views.py

from django.http import HttpResponse
from remote_ajax.http import HttpResponseAjax

def your_view(request):
    return HttpResponseAjax(HttpResponse("your response"))

JavaScript functions

django_remote_ajax.get

GET request
django_remote_ajax.get(url, succes_function, data);

django_remote_ajax.post

POST request
django_remote_ajax.post(url, succes_function, data);

Usage JavaScript

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title></title>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script type="text/javascript" src="http://localhost:8000/static/static/remote_ajax/main.js"></script>
  <script type="text/javascript">
  django_remote_ajax.get('http://localhost:8000/', function(data){$("#remote").html(data);})
  </script>
</head>
<body>
  <div id="remote"></div>
</body>
</html>