【问题标题】:How can I customise different views to the normal users and admin using django?如何使用 django 为普通用户和管理员自定义不同的视图?
【发布时间】:2022-06-20 14:07:20
【问题描述】:

我尝试了下面的视图功能。但是普通用户的页面“studenthpage”永远不会被渲染,而超级用户可以登录。而不是 'elif user.is_active:' 我什至尝试了其他条件,但仍然没有成功。谁能帮忙

def 登录(请求):

if request.method == 'POST':
    username = request.POST.get('username')
    password =request.POST.get('password')
    user = authenticate(request, username=username, password=password)
    if user is not None :
        auth_login(request,user)
        if user.is_superuser:               
            return redirect('adminhpage')
        elif user.is_active:
            return redirect('studenthpage')             
    else:
        messages.info(request, 'Username OR password is incorrect')
context = {}
return render(request, 'login.html', context)

【问题讨论】:

  • 普通用户究竟会发生什么?

标签: django django-authentication django-login


【解决方案1】:

我假设您可能以具有superuser 状态的user 身份登录,因此它总是以管理员身份登录。确保您登录时使用的user 没有superuser 状态,看看是否有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-22
    • 2015-10-07
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2019-09-18
    相关资源
    最近更新 更多