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 HttpRe开发者_运维知识库sponse('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?
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.
your last line in init() should be return options[action]
精彩评论