I am trying to make a application in django. I have an authentication api from another application. I take input from user via form and post the input to the api in json. After I get success message from api and my user is authenticated to api I want to make that user as request.user
in django.
Is there any way I can do it. I am thinking of doing it in middleware in process_view
. Is this right approach ? But I have no idea how to do it.
Any suggestion will be appriciated.
If you are really doing it thru Django views, you can manually login the user with:
# imports needed
from django.contrib.auth import login, logout
from django.contrib.auth.models import User
# hard way to login a user
user = User.objects.filter(username=INPUT_USERNAME).first()
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)
If you need to logout:
logout(request)
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.