Over the past four months I have been doing a lot of work in Python and Django. Naturally, while recently playing around with Appcelerator Titanium Desktop (which seems to have good Python support), I wanted to see how Django and Titanium would pair.
When a Titanium Desktop application (with Python support) is running, the Resources directory for the application is placed in the system Python path. Therefore, my first step was to place the main django directory in Resources. From there, I navigated to Resources and executed the startproject command:
1
$ django-admin.py startproject test_project
Next, I updated settings.py with information about my test database:
I made sure to include the full path to my test_project.db database, knowing that I would eventually need to refer to this path for Titanium Database support. Finally, I executed my syncdb command from within my test_project directory:
1
$./manage.pysyncdb
When prompted, I added a superuser account for myself.
Django Template Integration
Template integration was pretty smooth. What follows is a scaled down version of my index.html resource, and its associated files:
Resources >> index.html
12345
//Yes,this is all I have inmy main index.html resource<scripttype="text/python">fromtest_project.contrib.templateimportindexdocument.write(index())</script>
Database communication also went fairly smooth (though I did get tripped up trying to find a method to use an existing SQLite db for Titanium … ultimately the following worked for me:
1
Titanium.Database.openFile('/path/to/db'))
What follows are the code snippets which represent successful communication with the DB via Django, and via the Titanium Database API: