Django AMF service

I made a in Jaiku a few days ago that said the following,

Django amf service, once installed is a beauty to behold

Well the key part for me was “once installed”. I’ll attempt to explain how I got it working here.

is a middleware service for Django that allows you create AMF web services. Action Message Format is the web services format typically used by Flex applications by using the . Being able to support remoting web services in Django goes a long way to being able to integrate Flex applications with Django.

Download Django AMF from and install to your site-packages using setup.py. Then add ‘amf.django.middleware.AMFMiddleware’ to the MIDDLEWARE_CLASSES list in your settings. The key thing now is that you need to add the following to your settings,

AMF_GATEWAY_PATH = '/gateway/'
AMF_LOG_LEVEL = 'DEBUG'
AMF_LOG_FILE = 'log/amf.txt'
AMF_LOG_FILE_ENCODING = 'utf8'
AMF_LOG_FILE_MODE = 'a'

AMF_GATEWAY_PATH tells django that anything accessed under this path is an AMF service and gets passed into the middleware. e.g. my urls.py contains

(r'^/gateway/flashtest/test', 'my_app.views.flashtest'),

and that’s it!

I’m using the in Flex to talk to this service as follows:

var service:RemoteService = new RemoteService();
service.gatewayUrl = "http://localhost:8000/gateway/"
service.destination = "flashtest"
service.useAMF0=false
var token:AsyncToken = service.test(test_variable1, test_variable2);

Note that I’ve left the result handlers and assignment of the variables out for brevity, but hopefully you get the picture. The function flashtest gets called and the Flex data types get mapped to python data types as described .

It’s simplicity in design really is beautiful to behold.

10 Responses to “Django AMF service”


  1. 1 Steve

    Hi,

    I’ve just tried it myself. was about to go post on the sourceforge site…but trolling around google led me here.

    Are you using FlexBuilder? If you are, do you get a security sandbox violation when you try to run or debug the app from FlexBuilder?

    Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation:

    Just curious if you did, and if you did happen to — if you found a workaround ;)

    Cheers,
    Steve

  2. 2 Ben Cooper

    I didn’t see that error unfortunately. I must say though that it took me a while to figure out how to make the AMF call in Flex. I’m a Python developer and getting it going was my first ever use of Flex.

  3. 3 Brian Cragin

    Greetings, would it be possible to discuss your methods of connection more indepth via email?

  4. 4 Ben Cooper

    No problem. Please see my about page for contact information.

  5. 5 Arthur

    Dear Sir,
    I do the following steps but unfortunately kept getting error message,
    would you please help me?
    ———————–
    /www/Django# django-admin.py startproject calculate; cd calculate;
    /www/Django/calculate# md example

    — vi settings.py —
    #install applications
    INSTALLED_APPS = (
    ‘django.contrib.auth’,
    ‘django.contrib.contenttypes’,
    ‘django.contrib.sessions’,
    ‘django.contrib.sites’,
    ‘example’,
    )

    — vi /www/Django/calculate/example/views.py –
    def calculate(request, arg1, arg2): #1
    return arg1 + arg2
    def calculate2(request, obj): #2
    obj['sum'] = obj['arg1'] + obj['arg2']
    return obj
    def getCurrentDatetime(request): #3
    import datetime.datetime
    return datetime.now()
    def getAllCustomers(request): #4
    from example.models import Customer
    return Customer.objects.all()
    ————————————————————

    ————— vi urls.py ———————————————-
    from django.conf.urls.defaults import *
    import amf.django

    urlpatterns = patterns(”,
    (r’^gateway/calculateService/(.*)’, ‘amf.django.views’, {’views’:'example.views’}), #1

    (r’^gateway/timeService/whatTimeIsItNow’, ‘exampe.views.getCurrentDatetime’), #2
    )

    ———-test AMF in python ————————-
    import amf;
    service = amf.RemotingService(’http://bsd6.tv:8000/gateway/’, ‘calculateService’, 3);
    print service.calculate(2, 3);

    ===> Then, I get the error:

    Traceback (most recent call last):
    File “”, line 1, in ?
    File “/usr/local/lib/python2.4/site-packages/amf/__init__.py”, line 255, in __call__
    faultString=result.get(’description’, ”),
    AttributeError: ‘NoneType’ object has no attribute ‘get’
    —–

    Would you please give me some hints for debugging? Thanks a lots.

    – Arthur

  6. 6 Ben Cooper

    From a quick glance I can’t see the AMF_GATEWAY_PATH set in your settings. Try setting the following:

    AMF_GATEWAY_PATH = ‘/gateway/’

    AMF logging might also help:

    AMF_LOG_LEVEL = ‘DEBUG’
    AMF_LOG_FILE = ‘log/amf.txt’
    AMF_LOG_FILE_ENCODING = ‘utf8′
    AMF_LOG_FILE_MODE = ‘a’

  7. 7

    There’s now a generic AMF library for Python that you might want to try out (includes gateways for wsgi, django, twisted):

  8. 8 Brian Cragin

    This is our first Flex app. Its built using Modest maps, djangoAMF and Django. Please give it a test drive and tell us what you think.

  9. 9 Ben Cooper

    Here’s the thing. I’m sure it’s a problem with the Flash plugin, but I get annoyed by flash heavy interfaces which cause my browser to use almost 100% CPU and my Mac’s fans to start whirring loudly!

    Wouldn’t such an interface be better served with an AJAX style approach (with django serving JSON)? Check out django serialisation:

    On saying that, however, fantastic that we’ve got more and more people using django as a backend for fetching data. I’m particularly interested in using django for lightweight web services at the moment. Great stuff!

  10. 10

    @Brian Cragin - Great App!!! I’m especially enthused about it because I live in Encinitas, so this turns out be a SWEET resource for me personally. I’m also putting together some internal apps for my company here in the university city area, and I like your app layout concept, very good idea.

Leave a Reply


Spam Blocked

44,185 spam comments
blocked by
Akismet