
Django views serve nicely as service end-points for Flex applications. Here are some notes on maintaining authenticated sessions between a Flex/Air/Flash application and your Django backend.
gateway.py
from pyamf.remoting.gateway.django import DjangoGateway import myproject.myapp.views as views gw = DjangoGateway({ 'login' : views.login_user, 'logout' : views.logout_user, })
views.py
import pyamf from django.contrib import auth from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User try: pyamf.register_class( User, 'django.contrib.auth.models.User') except ValueError: print "Classes already registered" def logout_user(http_request): logout(http_request) def login_user(http_request, username, password): user = authenticate(username=username, password=password) if user is not None: login(http_request, user) return user return None @login_required def registered_user_protected_function(http_request): return "You are a registered user." @login_required def staff_protected_function(http_request): if http_request.user.is_staff != True: return None return "You are staff."
from flex
var netConnection:NetConnection = new NetConnection(); netConnection.connect("http://mysite.com/gateway"); var responder:Responder = new Responder(loginResult, handleFault); netConnection.call("login", responder, "username", "password")
The http_request carries a reference to the currently authenticated user throughout the session. This works for web based Flex application as well as AIR applications on the desktop. Note that I am using a try/except on the pyamf class registration calls. Because this is session based, the classes only need to be registered once. Without the trap, it throws a TypeError letting you know the registration has already taken place.
Django User Authentication Documentation
All of the various things you can do with authentication in Django. It is, of course, based mostly on the use of the very nice Django HTML template system. While those bits aren't handy to the likes of us, it is a good read either way.
pyAMF ByteArray example
This example shows the basic structure for setting up Django/Flex communication. It doesn't cover authentication, but covers a good bit of territory with examples in Flash and Flex.

Just found your useful hints, but I’m running in some trouble with login and the results…
How does your methods for loginResult and handleFault look like? And how do you call registered_user_protected_function from flex? Do you need to login again?
Hi Wolfgang,
I just noticed that I didn’t register the methods for registered_user_protected_function and staff_protected_function in the gateway.py in my example code. This is necessary, of course, to call them from Flex.
Since login returns a user, I usually have a corresponding class mapped on the Flex side as a VO. The result will be a UserVO in this case so your function would be like this:
private function handleLoginResult(user:UserVO) { if(user)//do some stuff }
Hi Joel
I am wondering the details of mapping classes between FLEX and python
for example how the register_class functions and how shall I do that in my application, if please would you send me your example on that, thanks alot.
Besides, recently I encountered problems when retreiving a list data in side another list from djangogateway to flex. the inner list was filtered so that I can’t get those data, do you know how to figure this out?
thanks a lot
i was thinking about cant we make the normal screen to work as touch screen applications ,my question is that i want to build the touch screen application for the normal screen which works like touchscreen so i saw this blog and i wanted to know this because many people say that there are touchscreen application and we have to use touch screen monitor built for those application if we can create the same this will be useful so that normal person can experience it right. so i had doubt in my mind ok i know u r the right person for this so could able to help me joel
Hi Albert,
I would highly recommend asking on the pyAMF User List. The developers are >extremely< helpful.
Also, look at the example files on the pyAMF web site. There are several related to mapping objects.
Joel,
Working for a large news organization for the past year I’ve created a dozen or so Flex applications that use Django with DjangoAMF on the back end. Recently I’ve been seeking a replacement for DjangoAMF, something that will work with Django 1.0. It is encouraging to see your success stories and helpful code samples posted here. Thank you for that, and keep up the good work.
Jay