0

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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

1 Comment

When I do so I got this error - Exception Value: __init__() should return None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.