简体   繁体   中英

How can check logged user for signin in django?

I am new in django and i am trying to create signup and sign in for user. i have finished for sign up page and some user are doing sign up that's stored in my database. When i am going to sign in it can't find those user whose are already sign up. Would you please help me for this?

my view.py

def login_user(request):
    state = "Please log in below..."
    if request.POST:
        email = request.POST.get('email')
        password = request.POST.get('password')        
        user = authenticate(email=email, password=password)
        if user is not None:
            if  request.user.is_authenticated:
                login(request, user)
                state = "You're successfully logged in!"
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = 'email: '+str(email)+' and password: '+str(password)+' is not found'

    return render_to_response('signup_test.html',{'state':state, 'email': email},context_instance=RequestContext(request))

The authenticate() method checks a username and a password .

You should modify this line:

user = authenticate(email=email, password=password)

to be:

user = authenticate(username=email, password=password)

However, this is assuming that you have set up authentication to accept an email as a username.

By default django support login with username so try some thing like

user = authenticate(username=username, password=password)
if user is not None:
    if user.is_active:
        login(request, user)
        # Redirect to a success page.
    else:
        # Return a 'disabled account' error message
        ...
else:

Follow this: https://docs.djangoproject.com/en/1.8/topics/auth/default/#authenticating-users

check your user in your database first

Remember to use the set_password() that inserts the encrypted password to create users. Example:

if form.is_valid():
    user = form.save(commit = False)
    user.set_password(form.cleaned_data['password'])
    user.save()

Login view

您只需将电子邮件字段设置为唯一,那么它应该只允许唯一用户,否则会引发验证错误,例如“使用此电子邮件的用户已存在”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM