首页  >  问答  >  正文

python django admin管理后台跳过登录,怎么做

django框架自带的admin管理后台,想做成不需要登录或者打开登录页面时,可以自动登录,
不知道如何下手,求大神指导

phpcn_u1582phpcn_u15822712 天前653

全部回复(1)我来回复

  • 给我你的怀抱

    给我你的怀抱2017-05-18 10:48:22

    摘自官方文档:
    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, login
    
    def 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页面就好了

    回复
    0
  • 取消回复