风向标 wrote:
>
>
> 程序的登陆部分是这样:
>
> def login(request):
> manipulator = AuthenticationForm(request)
> if request.POST:
> errors = manipulator.get_validation_errors(request.POST)
> if not errors:
> from django.contrib.auth import login
> login(request, manipulator.get_user()) ## 这里原来是
> 一个request.session的赋值.现在改用login函数
> request.session.delete_test_cookie()
> return HttpResponseRedirect('/')
> else:
> errors = {}
> request.session.set_test_cookie()
> return render_to_response('login.html', {'formvalue':Formnamevariable,
> 'form': forms.FormWrapper(manipulator, request.POST, errors)},
> context_instance=RequestContext(request))
>
> 这样能够正常登陆
>
> 注册部分是这样:
>
> def register(request, next_page=None):
> manipulator = RegisterManipulator(request)
> if request.POST:
> new_data = request.POST.copy()
> errors = manipulator.get_validation_errors(new_data)
> if not errors:
> userObj = manipulator.save (new_data)
> request.session[SESSION_KEY]=userObj.id
> return HttpResponseRedirect(next_page or '/')
> else:
> errors = {}
> return render_to_response('register.html',
> {'form': forms.FormWrapper(manipulator, request.POST, errors)},
> context_instance=RequestContext(request))
>
> 黑色部分我也用过以下代码代替:
>
>
> from django.contrib.auth import login
> login(request, userObj)
>
> 还是不行.
> 在RegisterManipulator是这样写的:
>
>
> class RegisterManipulator(forms.Manipulator):
>
> def __init__(self, request=None):
> self.request = request
> self.fields = [
> forms.TextField(field_name="username", length=18,
> maxlength=10, is_required=True,
> validator_list=[self.isValidUser]),
> forms.PasswordField(field_name="password", length=20,
> maxlength=24, is_required=True,
> validator_list=[self.isValidPassword ,
> validators.AlwaysMatchesOtherField('verifyPassword',
> consts.PWSSWORD_NOT_MATCH)]),
> forms.PasswordField(field_name="verifyPassword", length=20,
> maxlength=24, is_required=True,
> validator_list=[ self.isValidPassword]),
> forms.EmailField(field_name="email", length=20,
> maxlength=24, is_required=True,
> validator_list=[self.isValidEmail]),
> ]
> self.user_cache = None
>
> def isValidUser(self, field_data, all_data):
> if not field_data:
> raise validators.ValidationError, consts.USERNAME_EMPTY
> try:
> User.objects.get(username=field_data)
> except User.DoesNotExist:
> pass
> else:
> raise validators.ValidationError, consts.USERNAME_EXIST
>
> def isValidPassword(self, field_data, all_data):
> if not field_data:
> raise validators.ValidationError, consts.PWSSWORD_EMPTY
> if len(field_data) < 3:
> raise validators.ValidationError, consts.PWSSWORD_TOO_SHORT
>
>
> def isValidEmail(self, field_data, all_data):
> if not field_data:
> raise validators.ValidationError, consts.EMAIL_EMPTY
> try:
> User.objects.get(email=field_data)
> except User.DoesNotExist:
> pass
> else:
> raise validators.ValidationError, consts.EMAIL_EXIST
>
> def get_user_id(self):
> if self.user_cache:
> return self.user_cache.id
> return None
>
> def get_user(self):
> return self.user_cache
>
> def save(self, new_data):
> user_obj = User.objects.create_user(new_data["username"],
> new_data["email"], new_data["password"])
> user_obj.is_staff = True
> user_obj.is_superuser = False
> user_obj.save()
>
> return user_obj
>
> 我在AuthenticationForm中看到有这样一个函数authenticate() 因为它的处理
> user有了backend属性
>
> 而后在django.contrib.auth import login 中 这样的句子才会通过:
>
> request.session[BACKEND_SESSION_KEY] = user.backend
>
> 现在 我该怎么处理呢? 盼大牛解惑
>
> 谢谢
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-...@lists.python.cn
> Subscribe: send subscribe to python-chin...@lists.python.cn
> Unsubscribe: send unsubscribe to python-chin...@lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
我前几天刚刚升级到.95
在登录的地方也碰到这个问题,原因是.95在认证登录的部分有做调整。
为登录的部分专门提供了login和两个authenticate函数。
authenticate是用来进行认证的,通过认证的用户即可使用login来登录。
from django.contrib.auth import authenticate
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
else:
print "Your username and password were incorrect."
登录的时候直接使用login而无需.92中那样使用request.session[SESSION_KEY],
另外authenticate是可挂接(hook)的。
hook是我的了解,为什么呢?因为只要
通过在setting中设定:AUTHENTICATION_BACKENDS
即可使用用户自定义的认证方式来取代Django默认的认证方式,所以说是可挂接的。
这和.92中那种认证方式相比有很大的灵活性。
之前记得有Djangor问如何支持其他方式的认证,这在.95中是非常容易的事情:
只需要设定AUTHENTICATION_BACKENDS为你的新的认真模块即可!
同时新的认证模块必须提供两个方法:
get_user(id)和authenticate(**credentials):
get_user使用id返回一个User实例。
而authenticate这是用来认证的。
可以
定义为:
class auth_y_username:
def authenticate(username=None, password=None):
#if password and usename is right return a User
#else return None
也可以定义为
class auth_by_email:
def authenticate(email=None, password=None):
#if password and email are right return a User
#else return None
具体如何进行验证由程序员来控制。只是要记得在setting中告诉django:
AUTHENTICATION_BACKENDS = ('auth_by_username',)
或者
AUTHENTICATION_BACKENDS = ('auth_by_email',)
--
初从文,三年不中; 后习武,校场发一矢,中鼓吏,逐之出; 遂学医,有所成。 自撰一良方,服之,卒
个人blog: http://hackgou.itbbq.com
Email/Skype/MSN/QQ/Gtalk : HackGou#Gmail.com
PGP KeyID: hackgou AT Gmail.com
PGP KeyServ: subkeys.pgp.net