I am using Django built in view for user login:
url(r\'^user/login/$\', \'django.contrib.auth.views.login\', {\'template_name\': \'users/templates/login.html\'},
when you use the view build in django. and you are successfully logged in. django.contrib.auth.views.login which is the django view you used.
It has inside a parameter called LOGIN_REDIRECT_URL that automatically redirects you after logging in. and is by default configured with Default: '/ accounts / profile /'.
wich is The URL or named URL pattern where requests are redirected after login when the LoginView doesn’t get a next GET parameter. says documentation
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-LOGIN_REDIRECT_URL
Try adding this to the html page form you use to login.
Explained in doc:
(accounts/profile/ is ...) The URL where requests are redirected after login when the contrib.auth.login view gets no next parameter.
Because you are yet authenticated your request is redirected to this url. To avoid redirect you should logout user before send it to user/login, create your custom login view or append Next parameter.
All of this did not work for me. I used a JavaScript solution:
In the login.html (where the social login buttons are) I put this script which stores next in local storage::
<script>
// Check for local storage
if (typeof(Storage) !== "undefined") {
var h = window.location.href;
if (h.indexOf("next") > 0){
var tokens = h.split("=");
var nextUrl = tokens[1];
for(i = 2;i< tokens.length;i++){
nextUrl += ("=" + tokens[i]);
}
localStorage.setItem("next",nextUrl);
}
}
</script>
In the page where the users are being redirected to I used the following script to redirect:
$(document).ready(function () {
// Check for local storage
if (typeof(Storage) !== "undefined") {
var nextUrl = localStorage.getItem("next");
if (nextUrl.length > 0){
localStorage.setItem("next","");
window.location = nextUrl;
}
}
else {
// Sorry! No web storage support..
}
}
@CpS provided very good solution, I just want to enhance it a little bit:
For example, you have following urls:
urlpatterns = [
# django's login/logout
url(r'login/$', 'django.contrib.auth.views.login', name='login'),
url(r'logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'logout-then-login/$', 'django.contrib.auth.views.logout_then_login', name='logout_then_login'),
url(r'^$', views.dashboard, name='dashboard')
]
and dashboard.html is where you want to redirect after successful login, then you can modify your settings.py file accordingly:
from django.core.urlresolvers import reverse_lazy
LOGIN_REDIRECT_URL=reverse_lazy('dashboard')
LOGIN_URL=reverse_lazy('login')
LOGOUT_URL=reverse_lazy('logout')
We are using reverse_lazy() to build the URLs dynamically. The reverse_lazy() function reverses URLs just like reverse() does, but you can use it when you need to reverse URLs before your project's URL configuration is loaded.
Two ways, either you can use logout "LOGIN_REDIRECT_URL" or
just use the url which redirects to the view method which has @login_required decorator
example
for second login , after logout (below code is in logout.html)
<a href ="/">Login again </a>
In views.py
@login_required
def home(request):
return HttpResponse("Welcome")
django.contrib.auth.views.login redirects you to accounts/profile/ right after you log in.
You may want to set LOGIN_REDIRECT_URL inside your settings.py file to anything you like..