Our Blog
Pairing Django and Titanium in a Desktop App
Directory Layout
Creating the Project
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:
django-admin.py startproject test_project
Next, I updated settings.py with information about my test database:
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(os.path.dirname(os.path.dirname(__file__)), 'test_project.db'),
}
}
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:
./manage.py syncdb
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
#Yes, this is all I have in my main index.html resource
<script type="text/python">
from test_project.contrib.template import index
</script>
<script>
document.write(index())
</script>
-Resources
-test_project
-contrib
-template.py
def index():
kwargs = {'foo': 'bar'}
return _render_template('index', **kwargs)
def _render_template(template_name, **kwargs):
from django.template import loader, Context
t = loader.get_template('%s.html' % template_name)
c = Context(kwargs)
return t.render(c)
-Resources
-test_project
-templates
-base.html
-index.html
# base.html
{% block py_imports %}{% endblock %}
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<title>{% block title %}{% endblock %} | Titanium Testing</title>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
{% block js %}{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
# index.html
{% extends "base.html" %}
{% block title %}Template Communication {{ block.super }}{% endblock %}
{% block content %}
<strong>Foo</strong>: {{ foo }} <!-- Prints bar -->
{% endblock %}
Database Communication
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: 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:
-Resources
-test_project
-contrib
-db.py
-user.py
# db.py
def py_get_db():
from test_project import settings
return settings.DATABASES['default']['NAME']
# user.py
def py_update_name(username, first_name, last_name):
from django.contrib.auth.models import User
user = User.objects.get(username=username)
user.first_name = first_name
user.last_name = last_name
user.save()
def py_get_full_name(username):
from django.contrib.auth.models import User
user = User.objects.get(username=username)
return user.get_full_name()
-Resources
-test_project
-templates
-index.html (full version)
# index.html
{% extends "base.html" %}
{% block title %}Database Communication {{ block.super }}{% endblock %}
{% block py_imports %}
<script type="text/python">
from test_project.contrib.user import py_update_name, py_get_full_name
from test_project.contrib.db import py_get_db
</script>
{% endblock %}
{% block js %}
<script>
var username = 'leveille';
var first_name = 'Jason'
var last_name = 'Leveille'
/**
* Tell Django to update user first/last name
*/
py_update_name(username, first_name, last_name);
$(document).ready(function(){
/**
* Use Django to query the auth database
* and return the user fullname
*/
var $test_1 = $('#test_1');
$('button', $test_1).bind('click', function(){
$('.result', $test_1).text(py_get_full_name(username));
});
var $test_2 = $('#test_2');
$('button', $test_2).bind('click', function(){
var val = 'Empty result set';
try {
var db = Titanium.Database.openFile(py_get_db());
var rs = db.execute(
"SELECT first_name, last_name FROM auth_user "
+ "WHERE username = '"
+ username
+ "'"
);
if(rs.isValidRow()){
val = rs.field(0) + ' ' + rs.field(1);
}
} catch (err) {
val = err;
} finally {
rs.close();
db.close();
$('.result', $test_2).text(val);
}
});
});
</script>
{% endblock %}
{% block content %}
<div id="test_1">
<h1>Test 1</h1>
<p>
<button>Django Query for my Full Name</button>
</p>
<p>
<strong>Result</strong>:
</p>
</div>
<div id="test_2">
<h1>Test 2</h1>
<p>
<button>Titanium Query for my Full Name</button>
</p>
<p>
<strong>Result</strong>:
</p>
</div>
<div id="test_3">
<h1>Test 3</h1>
<p>
Passing Template Variables
</p>
<p>
<strong>Foo</strong>: {{ foo }}
</p>
</div>
{% endblock %}
The Result
If you have experience playing around with these two playfellows, feel free to share them here.
-->Other Recent Articles
-
A Mindset, Not a Process
-
Search Engine Optimization
-
Drexel University Launches Nursing Conferences Site
-
Blue Atlas Expands Web Team
-
Helping Our Area Schools
-
BluePlate: Now With Partial HTML5 Support
-
So You Want to be a Web Developer?
-
High School Football Scores Go Mobile!
-
Are Usability Studies Worth It?
-
BluePlate: The BlueAtlas Application Template
-
Welcome to the Blue Atlas Blog!

