问题
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'}, name='user-login'),
After login when I goto user/login again I can login second time. I submit the form and getting:
The current URL, accounts/profile/, didn't match any of these.
I haven't declare this url in urls.py.
What I am doing wrong? Why framework want to redirect to this url?
回答1:
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..
回答2:
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.
回答3:
@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.
回答4:
check your login.html
<form method="post" action=".">
~blah~
<input type="hidden" name="next" value="/"><!-- login.html must send "next" parameter, and "next" parameter value is url that you want to redirect. -->
~blah~
</form>
good luck~
回答5:
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..
}
}
来源:https://stackoverflow.com/questions/13186693/why-second-user-login-redirects-me-to-accounts-profile-url