tags:

views:

55

answers:

2

views.py:

class ajax_profile():

def __init__(self, request):

    username = request.REQUEST.get('username','')
    email = request.REQUEST.get('email','')
    password = request.REQUEST.get('password','')
    action = request.REQUEST.get('action','')

    options = { 
               'check_username'    : self.check_username(username), 
               'check_email'       : self.check_email(email),
               'forgot_password'   : self.forgot_password(email),
               'login'             : self.login(username, password),
               }
    options[action]

def check_username(self, username):
    return HttpResponse('Test %s' % username)

def check_email(self, email):
    pass

def forgot_password(self, email):
    pass

def login(self, username, password):
    pass

urls.py

(r'^ajax_profile/$', 'apps.profiles.views.ajax_profile'),

URL to call

ajax_profile/?action=check_username&username=testtest

ERROR: instance has no attribute 'status_code'

Why?

A: 

your last line in init() should be return options[action]

Ryan Nowakowski
When I do so I got this error - Exception Value: __init__() should return None
pkdkk
-1. `__init__` must never return a value.
Daniel Roseman
Thanks -I'm a newbee..
pkdkk
+2  A: 

I don't recommend doing things this way. Your views should return HttpResponse objects, while ajax_profile's init method should initialize an instance of ajax_profile.

If you must, you can try having ajax_profile subclass HttpResponse, and use super to initialize the HttpResponse at the end of ajax_profile's __init__:

class ajax_profile(HttpResponse):
    def __init__(self, request):
        # ...
        response_text = options[action]()
        super(ajax_profile, self).__init__(response_text)

Also worth noting, the way options is set up, every method in the dictionary (check_username, check_email, etc) will be run every time regardless of the action. Probably not what you want.

tcarobruce