- Author:
- davidblewett
- Posted:
- June 10, 2008
- Language:
- Python
- Version:
- .96
- Score:
- 1 (after 1 ratings)
This is a simple view I'm using to log people into our app using ExtJS's AJAX form submission.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.utils import simplejson
def ext_login(request):
json = {
'errors': {},
'text': {},
'success': False,
}
user = authenticate(username=request.POST['username'],
password=request.POST['password'])
if user is not None:
if user.is_active:
login(request, user)
json['success'] = True
json['text']['welcome'] = 'Welcome, %s!' % (user.get_full_name(),)
else:
# Return a 'disabled account' error message
json['errors']['username'] = 'Account disabled.'
else:
# Return an 'invalid login' error message.
json['errors']['username'] = 'Username and/or password invalid.'
return HttpResponse(simplejson.dumps(json, cls=DjangoJSONEncoder))
Sample Ext Panel config, uses Ext.ux.PasswordField
(http://extjs.com/forum/showthread.php?t=20417):
Signet.LoginForm = Ext.extend(Ext.FormPanel, {
labelAlign: 'right'
,labelWidth: 75
,title: 'Home'
,xtype: 'form'
,initComponent:function() {
Ext.apply(this, {
buttons:[
new Ext.Button({
handler: this.authSubmit
,scope: this
,text: 'login'
})
]
,items:[
new Ext.form.TextField({
fieldLabel: 'User Name'
,name: 'username'
,width: 100
})
,new Ext.ux.PasswordField({
fieldLabel: 'Password'
,name: 'password'
,showCapsWarning: true
,showStrengthMeter: false
,width: 100
})
]
});
Signet.LoginForm.superclass.initComponent.apply(this, arguments);
if (this.initialConfig.handler && this.initialConfig.scope){
this.form.on('actioncomplete', this.initialConfig.handler, this.initialConfig.scope);
this.form.on('actionfailed', this.initialConfig.handler, this.initialConfig.scope);
}
}
,authSubmit: function(){
this.form.submit({
clientValidation: false
,method: 'POST'
,url:'/csds/ext/login/'
});
}
});
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 9 months, 3 weeks ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
- Help text hyperlinks by sa2812 11 months ago
Comments
Please login first before commenting.