[Answer]-Django API on WordPress website

1👍

The basic structure would look like this:

On the Python/Django Side

Make a Django view that takes a few arguments. For example, if I had a database hooked up to a Django project that had a bunch of data about store sales (including the day and time they occurred), I could let my WordPress site retrieve all sales in a given month with something like http://www.mydjangoproject.com/api/<month>/<year>/. My request would be routed to my Django API view, the view would take the month and year and then query the DB for all sales in that month. It would then encode that information as JSON, and render it.

On the PHP/Wordpress Side

Now, on my WordPress site, I can load the transactions from a given month like this:

# get the transactions for December 2013
$month = 12
$year = 2013
$JSON = file_get_contents(http://www.mydjangoproject.com/api/$month/$year/);

# associative array of transactions
$transactions = json_decode($JSON);

Then, you can use the $transactions array to do whatever you need with it.

Leave a comment