2010-11-24 8 views

答えて

2

一意のメールアドレスを適用する場合は、おそらくこれを行うことができます。つまり、ユーザーは同じ電子メールアドレスを持つことはできません。 。

def login(request): 
    username = request.POST['username'] 
    password = request.POST['password'] 
    user = User.objects.filter(email = username)[0] 
    if(user is not None): 
     # -- the user was retrieved by an email address 
     # -- now you can authenticate and log them in log them in 
     from django.contrib import auth 
     user = auth.authenticate(user.username, password) 
     if(user is not None): 
       auth.login(user, request) 
:viewメソッドは次のようになります

<form method="post" action="{% url myproject.views.login %}"> 
    <p>Username</p> 
    <input type='text' name='username'/> 

    <p>Password</p> 
    <input type='password' name='password'/> 
    <input type="submit" value="Login"/> 
</form> 

:あなたは電子メールアドレスでユーザーを取得し、それらをログに記録可能性があり、このよう

フォームは次のようになります。

OpenIDで行くための別の方法かもしれない:http://bit.ly/aOaAbw

http://bit.ly/a2OlHX

は、ユーザーごとに一意の電子メールアドレスを確認してください

+0

これは機能する場合があります。また、自分の認証バックエンドを作成し、それをミドルウェアとして含めることも知っていました。 – chiurox

+0

見つけたソリューションのリンクがあります。この質問に答えると、私は電子メールでサインインを実装することも検討しています。 – JeremyFromEarth

0

私は自分の問題を解決したと思いますが、少なくとも今は機能的です。 自分の認証バックエンドを使用することに決めました。私はファイル 'auth_backends.py'を作成し、それを私のsettings.pyのAUTHENTICATION_BACKENDSに追加しました:

私のログインフォームフィールドには、「ユーザー名」とパスワードのみが含まれています。入力されたユーザー名が実際にユーザー名か電子メールかどうかを確認するための唯一の方法は、.find( '@')を実行することです。 これを確認するより良い方法はありますか?これで十分でしょうか? 私がこれをやっている理由は、ユーザーが自分のユーザー名(実際には数字で構成される「ID」)よりも自分のメールアドレスを覚えやすいからです。

私はまた、重複した電子メールの世話をする必要があります。

from django.conf import settings 
from django.contrib.auth.backends import ModelBackend 
from django.core.exceptions import ImproperlyConfigured 
from django.db.models import get_model 
from django.contrib.auth.models import User 

class CustomUserModelBackend(ModelBackend): 

def authenticate(self, **credentials): 
    if 'username' in credentials: 
     if credentials['username'].find('@') > 0: 
      return self.authenticate_by_email(**credentials) 
     else: 
      return self.authenticate_by_username(**credentials) 

def authenticate_by_username(self, username=None, password=None): 
    try: 
     user = User.objects.get(username=username) 
     if user.check_password(password): 
      return user 
    except User.DoesNotExist: 
     return None 

def authenticate_by_email(self, username=None, password=None): 
    try: 
     user = User.objects.get(email=username) 
     if user.check_password(password): 
      return user 
    except User.DoesNotExist: 
     return None 
関連する問題