Chr*_*ten 10 python django django-admin django-views
我正在开发一个Django应用程序,它有两种类型的用户:管理员和用户.两者都是我项目中的组,并且根据登录的个人属于哪个组,我想将它们重定向到单独的页面.现在我在settings.py中有这个
LOGIN_REDIRECT_URL = 'admin_list'
Run Code Online (Sandbox Code Playgroud)
这会重定向所有登录"admin_list"的用户,但该视图只能由Admins组的成员访问 - 否则返回403.至于登录视图本身,我只是使用Django提供的那个.我已将此添加到我的主urls.py文件中以使用这些视图:
url(r'^accounts/', include('django.contrib.auth.urls')),
Run Code Online (Sandbox Code Playgroud)
如何进行此操作以便只将Admins组的成员重定向到此视图,并将其他所有人重定向到其他视图?
Ala*_*air 22
创建一个单独的视图,根据用户是否在管理员组中重定向.
from django.shortcuts import redirect
def login_success(request):
"""
Redirects users based on whether they are in the admins group
"""
if request.user.groups.filter(name="admins").exists():
# user is an admin
return redirect("admin_list")
else:
return redirect("other_view")
Run Code Online (Sandbox Code Playgroud)
将视图添加到您的urls.py,
url(r'login_success/$', views.login_success, name='login_success')
Run Code Online (Sandbox Code Playgroud)
然后用它来进行LOGIN_REDIRECT_URL设置.
LOGIN_REDIRECT_URL = 'login_success'
Run Code Online (Sandbox Code Playgroud)
我使用中间视图来完成同样的事情:
LOGIN_REDIRECT_URL = "/wherenext/"
Run Code Online (Sandbox Code Playgroud)
然后在我的 urls.py 中:
(r'^wherenext/$', views.where_next),
Run Code Online (Sandbox Code Playgroud)
然后在视图中:
@login_required
def wherenext(request):
"""Simple redirector to figure out where the user goes next."""
if request.user.is_staff:
return HttpResponseRedirect(reverse('admin-home'))
else:
return HttpResponseRedirect(reverse('user-home'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4842 次 |
| 最近记录: |