
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.

