2

フォームから入力を受け取り、データベースからデータを検証し、正しい場合は right.html を返し、そうでない場合は wrong.html を返すログインおよび認証機能があります。パスワードは galaxy_user データベースに保存されます。uname の出力は次のとおりです。

u'sachitad'

passw の出力は次のとおりです。

u'f8566297ee28e8a3096497070b37b91d24c24243'



 def login(request):
        if request.method == 'POST':
            username = request.POST['username']
            password = request.POST['password']
            u = ''.join(username)
            p = hashlib.sha1(password).hexdigest()
            a = GalaxyUser.objects.values_list('username', 'password')
            uname = a[0][0]
            passw = a[0][1]
            user = authenticate(uname=u, passw=p)

            if user is not None:
                return render_to_response('right.html', context_instance=RequestContext(request))

            else:
                return render_to_response('wrong.html', context_instance=RequestContext(request))

        else:
            return render_to_response('login.html', context_instance=RequestContext(request))

編集: GalaxyUser テーブル:

 id | create_time         | update_time         | email                         | password                                 | external | deleted | purged | username | form_values_id | disk_usage |
+----+---------------------+---------------------+-------------------------------+------------------------------------------+----------+---------+--------+----------+----------------+------------+
|  1 | 2013-01-11 15:00:30 | 2013-01-11 15:00:30 | user1@gmail.com   | f8566297ee28e8a3096497070b37b91d24c24243 |        0 |       0 |      0 | sachitad |           NULL |       NULL |
|  2 | 2013-01-11 15:01:01 | 2013-01-11 15:01:01 | user2@zurelsoft.com | f8566297ee28e8a3096497070b37b91d24c24243 |        0 |       0 |      0 | saugat   |           NULL |       NULL |
+----+---------------------+---------------------+-------------------------------+------------------------------------------+----------+---------+--------+-

メールとパスワードで認証したい。

4

2 に答える 2

2

Djangoは、指定されたアルゴリズム('md5'、'sha1'、または'crypt')を使用して、指定されたプレーンテキストパスワードとソルトのhexdigestとしてパスワードを保存します

authenticate()は、ハッシュバージョンではなく、ユーザーの登録時に指定したパスワードを取得します。ユーザーを認証するには、プレーンテキストのパスワードの認証を提供する必要があります。

于 2013-01-11T07:14:11.290 に答える
0

authenticate()は 2 つのキーワード引数を取りusernameますpassword

user = authenticate(username=u, password=p)


編集:

デバッグ情報がないため、何が問題なのかを判断するのは困難です...登録機能でユーザーを正しく登録していない可能性があり、ユーザー名またはパスワードを正しく設定していない可能性があります。これにより、wrong.html ページにリダイレクトされる理由が説明されます。次のログイン機能が機能します。

from django.shortcuts import render_to_response, redirect
from django.http import HttpResponseRedirect
from django.template import RequestContext
from somewhere.forms import LoginForm
from django.contrib.auth import authenticate, login

def Login(request):
    if request.method == 'POST':
        login_form = LoginForm(request.POST) # Bound LoginForm
        if login_form.is_valid():
            username = login_form.cleaned_data['username']
            password = login_form.cleaned_data['password']
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect('/success/')
                else:
                    # user has not been activated...must activate using the following lines:
                    #### request.session['username'] = username
                    #### user.backend = 'django.contrib.auth.backends.ModelBackend'
                    #### user.is_active = True
                    #### user.save()
            else:
                # u_and_p_error = "Username and password did not match"
        else:
            return render_to_response('index.html', {'login_form': login_form}, context_instance=RequestContext(request))
于 2013-01-11T07:00:30.033 に答える