Search code examples
pythondjangodjango-socialauth

How to seperate login redirect urls in django


I have built a django app.Can someone help me in seperating the redirect URLs while logging in from facebook(django-social-auth) and a normal login form. I have two types of users, one which I need to login using facebook and the other using the form. I have defined different URLs for both the purposes and defined it in my views.py Also given SOCIAL_AUTH_LOGIN_REDIRECT_URL in my settings.py, but when logging in using the form, it is also getting redirected to the same URL. Please help. Relevant code is attached Thanks in advance.

views.py:

def admin_dashboard(request):
    if request.user.id!=None and request.user.usertype.description=="ADMIN" and request.user.is_authenticated:
        if request.method=="GET":
            return render_to_response('testu/admin_dashboard.html',context_instance=RequestContext(request))


def student_dashboard(request):
    if request.user.id!=None and request.user.usertype.description=="STUDENT" and request.user.is_authenticated:
        if request.method=="GET":
            return render_to_response('testu/student_dashboard.html',{"tests":Test.objects.all()},context_instance=RequestContext(request))

urls.py:

urlpatterns = patterns('',
    url(r'', include('social_auth.urls')),
    url(r'^admin_dashboard/',admin_dashboard),
    url(r'^student_dashboard/',student_dashboard),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py:

FACEBOOK_APP_ID = '...'
FACEBOOK_API_SECRET = '...'
FACEBOOK_EXTENDED_PERMISSIONS = ['email']
SESSION_SERIALIZER='django.contrib.sessions.serializers.PickleSerializer'
SOCIAL_AUTH_NEW_USER_REDIRECT_URL = '/student_dashboard/'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/student_dashboard/'
SOCIAL_AUTH_USER_MODEL = 'myapp.customuser'

basically, whenever im signing in with user of "ADMIN" description, even that is being redirected to 'student_dashboard'


Solution

  • Did you try having a common login Redirect view. Ex:

    LOGIN_REDIRECT_URL = '/my_common_view/'
    
    def my_common_view(request):
        if user is student:
            redirect to student dashboard
        else:
            redirect to admin dashboard
    

    This should solve this problem.