6

So I want to redirect after login to the same page. But Django Social Auth isn't redirecting. It redirects to "/pins/#_=_

Html:

<a href="{% url socialauth_begin 'facebook' %}?next={{ request.get_full_path }}">Facebook Login</a>

Urls.py

url(r'^$', 'pinry.core.views.home', name='home')

Views.py

def home(request):
    if request.user.is_authenticated():
        if 'next' in request.GET:
            return HttpResponseRedirect(request.GET['next'])
        else:    
            return HttpResponseRedirect('/pins'))
    return HttpResponseRedirect(reverse('core:concept'))

Settings.py

LOGIN_URL          = '/login-form/'
LOGIN_REDIRECT_URL = '/'
LOGIN_ERROR_URL    = '/login-error/'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_NEW_USER_REDIRECT_URL = '/pins/'
SOCIAL_AUTH_NEW_ASSOCIATION_REDIRECT_URL = '/new-association-redirect-url/'
SOCIAL_AUTH_DISCONNECT_REDIRECT_URL = '/account-disconnected-redirect-url/'
SOCIAL_AUTH_BACKEND_ERROR_URL = '/new-error-url/'

SOCIAL_AUTH_COMPLETE_URL_NAME  = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'socialauth_associate_complete'
SOCIAL_AUTH_DEFAULT_USERNAME = 'new_social_auth_user'

TEMPLATE_CONTEXT_PROCESSORS = (
...
    "social_auth.context_processors.social_auth_by_name_backends",
    'social_auth.context_processors.social_auth_backends',
    'social_auth.context_processors.social_auth_by_type_backends',
    'social_auth.context_processors.social_auth_login_redirect',
    "django.core.context_processors.csrf"
    )

2 Answers 2

8

I faced the same issue as yours I solved it by changing my login button URL from

<a href="{% url 'social:begin' 'github' %}>

to

<a href="{% url 'social:begin' 'github' %}?next={{ request.GET.next }}>

3
  • what is request.GET.next in that case?
    – tree em
    May 28, 2020 at 16:06
  • @kn3l the request.GET.next will be whatever you have set for next in query parameters. Eg: www.example.com?next=/home/ so the next will be /home
    – thisshri
    Jul 9, 2021 at 8:09
  • In my case ?next={{ request.path }} worked as expected.
    – Paloha
    May 31 at 7:58
0

change LOGIN_REDIRECT_URL = '/pins/' to LOGIN_REDIRECT_URL = '/', or whatever u want to redirect this propety tells where to redirect after login

2
  • Added my urls.py and views.py. How can I pass the next parameter in GET request after login by DSA? Presently it's not there hence its redirecting to "/pins"
    – Coderaemon
    Oct 26, 2014 at 22:04
  • 1
    This doesn't answer the question. What he wants do is if the user is redirected to login page from some other page like pictures/nature, he wants the user to go to that particular page picutres/nature.
    – samsri
    Jan 2, 2019 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.