34👍
✅
Create a separate view that redirects user’s based on whether they are in the admin group.
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")
Add the view to your urls.py
,
url(r'login_success/$', views.login_success, name='login_success')
then use it for your LOGIN_REDIRECT_URL
setting.
LOGIN_REDIRECT_URL = 'login_success'
5👍
I use an intermediate view to accomplish the same thing:
LOGIN_REDIRECT_URL = "/wherenext/"
then in my urls.py:
(r'^wherenext/$', views.where_next),
then in the view:
@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'))
- Using bundle_files = 1 with py2exe is not working
- When is it appropriate to use Django context processors?
- How can I create a case-insensitive database index in Django?
0👍
from django.shortcuts import redirect
def redirect_response(request, **kwargs):
if kwargs.get('something'):
redirect(to='https://example.com/foo')
else:
redirect(to='index')
urlpatterns = [
path('redirect', redirect_response, name='redirect'),
path('index',index_response ,name='index')
]
- Can you declare multiple with variables in a django template?
- Django request Post json
- Django REST Framework (ModelViewSet), 405 METHOD NOT ALLOWED
Source:stackexchange.com