您的位置:首頁技術文章
文章詳情頁

python django admin管理后臺跳過登錄,怎么做

瀏覽:129日期:2022-07-24 18:31:19

問題描述

django框架自帶的admin管理后臺,想做成不需要登錄或者打開登錄頁面時,可以自動登錄,不知道如何下手,求大神指導

問題解答

回答1:

摘自官方文檔:login(request, user, backend=None)

To log a user in, from a view, use login(). It takes an HttpRequest object and a User object. login() saves the user’s ID in the session, using Django’s session framework.

Note that any data set during the anonymous session is retained in the session after a user logs in.

This example shows how you might use both authenticate() and login():

from django.contrib.auth import authenticate, logindef my_view(request): username = request.POST[’username’] password = request.POST[’password’] user = authenticate(request, username=username, password=password) if user is not None:login(request, user)# Redirect to a success page.... else:# Return an ’invalid login’ error message....

知道了上面這些之后,你自己就可以做個用來登錄的view,在里面登錄完之后再redirect到admin頁面就好了

標簽: Python 編程
相關文章:
国产综合久久一区二区三区