Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically authenticate a user in Django?

How can I log-in the user programmatically in Django? I have the username and password of the User. Is there a method that let's me log him in?

like image 763
Mridang Agarwalla Avatar asked Oct 25 '11 17:10

Mridang Agarwalla


People also ask

What does authenticate function do in Django?

Authentication Views Django provides several views that you can use for handling login, logout, and password management. These make use of the stock auth forms but you can pass in your own forms as well. Django provides no default template for the authentication views.


1 Answers

There is no other way than "programmatically". Of course, this is documented.

from django.contrib.auth import authenticate, login  user = authenticate(username=username, password=password) if user is not None:     login(request, user) 
like image 58
Cat Plus Plus Avatar answered Oct 10 '22 08:10

Cat Plus Plus