1

I want to login with handler.

I have a code use session but i want to use handler:

I have visit :

https://docs.djangoproject.com/en/1.11/topics/auth/default/

But i don't understand complete.

I want to log user (with username or email and password)

Do you have a code for example or project in stackoverflow or github or . . . ???

8
  • 1
    what you don't understand and what exactly do you want from auth Commented Jun 23, 2017 at 15:16
  • 1
    I want to log use.i don't understand loger! Commented Jun 23, 2017 at 15:17
  • 1
    what's a loger? Commented Jun 23, 2017 at 15:18
  • 1
    I dont understand how it work!where should i import?! Commented Jun 23, 2017 at 15:19
  • 1
    log user means you want to create a log file and login means something else. I don't understand if you are saying log the user or login the user? Commented Jun 23, 2017 at 15:20

1 Answer 1

1

login the user is easy if you are using default user model from django.contrib.auth.models

from django.contrib.auth import authenticate, login
def user_login(request):
    # check here that request.method is POST or not.
    user = authenticate(username=request.POST.get('username'), password=request.POST.get('password'))
    if user is not None:
        login(request, user)
        # send some http response here that login successful or redirect to some other page

    else:
        # return an error page saying that username password not correct

authenticate function will check for username and password in User table in the database if it founds a user matching query then it returns the user object else it will return None. You might not want to manage sessions as django already sets a cookie for every user that successfully logs in so if user has logged in once then he will not be required to enter password again.

Sign up to request clarification or add additional context in comments.

3 Comments

It will not set session?just return user's object?
it won't return username and password. session is set in request object and when you send any response from django after login it will set session in the browser
Tnx a lot.when i use this answer.i will accept and vote up it.(now i am with phone)💛

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.