I'm playing around with Django (and considering whether to use it to build the back-end for Singularity). I love the regular expressions-based Front Controller that Django uses and the first thing I thought when I saw it was that it would be incredibly easy to use this to implement deep linking in Flex and Flash applications. I whipped up a proof-of-concept a few moments ago which I thought I'd share with you.
Note: This is in no way meant to be a suggested solution to deep-linking (it was more of an exercise in playing with Django for me). Take a look at SWFAddress for a JavaScript-based way of achieving the same goal.
If you're going to follow along, you'll need Adobe Flex (2 or 3), a copy of SWFObject, and a working Django installation (here's how I got started with Django on OS X).
Flex:
Create a new Flex application called DjangoSWF with the following code and publish it.
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Label text="{Application.application.parameters.url}"/> </mx:Application>
All this application does is read the value of a FlashVar parameter called url and display it in a Label component.
Next, let's write some Python code so Django can dynamically populate the URL based on the actual URL that the user entered.
Django:
Start a new Django project: django-admin.py startproject djangoswf.
Create two folders in the project folder called templates and static.
In the templates folder, create a file called djangoswf.html:
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{{url}}</title>
<script src="/static/swfobject.js" language="javascript"></script>
</head>
<body scroll="no">
<div id="djangoswf">Django SWF example. This requires Flash Player.</div>
<script language="JavaScript" type="text/javascript">
/*<!--*/
var so = new SWFObject("/static/DjangoSWF.swf", "theswf", "100%", "100%", "9");
so.addVariable("url", "{{url}}");
so.write("djangoswf");
/*-->*/
</script>
</body>
</html>
In the static folder, place the Flex SWF (DjangoSWF.swf) and the SWFObject.js file.
Create a views.py file in your project:
from django.shortcuts import render_to_response def flash_url(request, url): return render_to_response("djangoswf.html", {"url": url})
This is your view, where all the heavy-lifting is done (confusingly, Django calls its Business Delegates "Views"). Basically, that one line of code renders your HTML template and passes a url argument to it. In the template, that URL is passed to your Flex SWF via FlashVars (using the addVariable method of your SWFObject instance).
Next, edit the urls.py file:
from django.conf.urls.defaults import * from djangoswf.views import * urlpatterns = patterns('', (r'^static/(?P <path>.*)$', 'django.views.static.serve', {'document_root': 'static'}), (r'^(.*)', flash_url), )
This is where the magic happens. The Django Front Controller maps URLs to business methods using regular expressions.
The first URL mapping is for static content. Normally, you would use a web server like Apache to serve static content in a deployment environment, but for development purposes it makes sense to have Django serve them. The static content in this example is the SWFObject.js file and the Flex SWF.
The next line contains the deep-linking mapping for the Flex application. All it does is take any URL and passes it to the flash_url method as an argument. (The ordering of the lines is important as the URL mappings use short-circuit logic, starting from the top.)
Finally, run the development server (python manage.py runserver) and hit http://localhost:8000/some/really/deep/url to see the current URL getting passed to Flex.
In a real application, of course, you would change the application state to reflect the URL instead of merely displaying it.
If you're interested in deep linking in your Flash and Flex applications, check out SWFAddress.
The Implementing simple deep-linking in Flex (and Flash) using Django article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.
Thanks for sharing Aral..
As long as you’re playing with Flex/Django you might want to take a look at pyamf (basically amfphp for python); they already have an example Django gateway:
http://pyamf.org/wiki/DjangoHowto
Hi cynic,
Been playing with it :)
Don’t work for me. My Browser show me a single message “Django SWF example “This requires Flash Player.”. In urls.py I need to teel django the true path to static folder.
How to do it without python?
I’m using SwfAddress and dunno how to get really deeeeep linking
Fantastic idea but with SWF 2.0 it needs something instead of addVar:
var flashvars = {url: “{{url}}”};
swfobject.embedSWF( ‘/static/DjangoSWF.swf’,
‘djangoswf’,
‘500′, ‘500′,
‘9.0.45′,
‘expressinstall.swf’,
flashvars,
{ bgcolor: ‘#FFFFFF’, menu: ‘false’, allowfullscreen: ‘false’ },
{id: ‘djangoswf’});
Flex code remains the same :D
its a pleasure to work with django and flash/flex…
:)
Hi
I am getting this error when I tried ur example, I like to know where I went wrong
djangoswf.htmlRequest Method: GET
Request URL: http://localhost:8000/
Exception Type: TemplateDoesNotExist
Exception Value: djangoswf.html
Exception Location: C:\Python25\Lib\site-packages\django\template\loader.py in find_template_source, line 74
Python Executable: C:\Python25\python.exe
Python Version: 2.5.0
Thiru
Thiru, I remember seeing the TemplateDoesNotExist exception on GAE. It was, if I remember correctly, an issue with GAE itself (not my code) and was temporary. Would love to hear if you make and progress. G’luck.
Hello,
actually there is a library for navigation in flex and air application support for: deep linking, google analytic, and user authorization.
http://code.google.com/p/ticlib/