47

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?

the
  • 21,007
  • 11
  • 68
  • 101
Codium
  • 3,200
  • 6
  • 34
  • 60

6 Answers6

108

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..

the
  • 21,007
  • 11
  • 68
  • 101
Traian
  • 2,915
  • 1
  • 24
  • 32
  • 8
    This is correct, although it's weird it's coded to redirect somewhere that doesn't exist. Surely redirecting to '/' would be a more sensible default? – Rob Grant Jul 16 '14 at 10:03
  • Worked great. Adding the text "LOGIN_REDIRECT_URL = ('..')" to settings.py worked for me to redirect the user back to the default main page. – Praxiteles Jan 06 '15 at 01:00
  • 1
    it says `This page isn’t working localhost redirected you too many times.` – Ishan Srivastava Feb 01 '18 at 12:22
8

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.

Community
  • 1
  • 1
dani herrera
  • 48,760
  • 8
  • 117
  • 177
3

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~

chobo
  • 4,830
  • 5
  • 23
  • 36
1

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..
    }
}
Avia
  • 1,829
  • 2
  • 14
  • 15
0

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.

-1

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")