Django redirect after user successfully logs in
NickName:Praful Bagai Ask DateTime:2015-07-07T22:52:28

Django redirect after user successfully logs in

I want to redirect the user after successful login. Currently, I'm doing it this way. PS: I don't want to use Django Forms. Is the way I'm doing this the standard way of redirecting the user?

HTML

<form role="form">
    <div class="form-group">
        <label for="sender-email" class="control-label">Username:</label>
        <div class="input-icon"> <i class="icon-user fa"></i>
            <input id="sender-email" type="text" placeholder="Username" class="form-control email">
        </div>
    </div>
    <div class="form-group">
        <label for="user-pass" class="control-label">Password:</label>
        <div class="input-icon"> <i class="icon-lock fa"></i>
            <input type="password" class="form-control" placeholder="Password" id="user-pass">
        </div>
    </div>
    <div class="form-group">
        <a href="#" class="btn btn-primary btn-block" id="authenticate_user">Submit</a>
    </div>
</form>

js

$(function(){
    $('#authenticate_user').click(function(){
        username = $('#sender-email').val();
        password = $('#user-pass').val();
        $.ajax({
            type : "POST",
            url: '/login',
            data: {'username':username, 'password':password},
            success : function(result){
                window.location = '/';
            },
            error: function (xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
            },
        });
    })
})

views.py

@csrf_exempt
def signin(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('%s' % (settings.LOGIN_REDIRECT_URL))
            else:
                return {'error': 'Your account has been disabled.'}
        else:
            return {'error': 'Invalid Login.'}

    if request.user.is_authenticated():
        return redirect('%s' % (settings.LOGIN_REDIRECT_URL))

    ctx = {}
    return render(request, 'login/login.html', ctx)

Copyright Notice:Content Author:「Praful Bagai」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31272082/django-redirect-after-user-successfully-logs-in

More about “Django redirect after user successfully logs in” related questions

Django redirect after user successfully logs in

I want to redirect the user after successful login. Currently, I'm doing it this way. PS: I don't want to use Django Forms. Is the way I'm doing this the standard way of redirecting the user? HTML...

Show Detail

django - redirect to a page after registering a user

After a user successfully registers an account (creates a username and password) the webpage returns a blank registration form. I would like to redirect to the 'landingpage' url after successful

Show Detail

Django redirect user after login

I have a problem when trying to redirect a user after login to the base.html which is in main template folder. Django can't find this template. I get the error: Django tried these URL patterns, in

Show Detail

redirect certain user to certain view in django

i need to know how to redirect the user in Django views to a certain page after he logs in. let's say we have 3 types of users and 3 types of pages, i want each type to be directed to a certain pa...

Show Detail

In Django, after login redirect the user to the previous page

I want to secure each webpage of my django app, and what I want is that after login the user should be redirected to the previous page he was trying to access. So in accounts/views.py this is the l...

Show Detail

how to redirect the user to user-specific url after login django?

I am new in django. I am trying to create a user specific page where after login the user land to somthing like mydomain.com/dashboard/. I am trying to implement the following solution Django - after

Show Detail

Redirect To Page Besides Default After User Logs In

I am using Visual Studio 2012, and created a project using the "ASP.NET Web Forms App" (Not the empty project). The project handles creating new logins, and changing passwords by default. After a u...

Show Detail

Redirect to previous user's last page after new user login

I am currently developing a Django website and I am using django.contrib.auth and django-security-session for closing user sessions automatically. In case a user leaves its session open and a new...

Show Detail

Express - trying to redirect after user successfully logs in

I'm clearly not getting some core Node concept. After validating a user from the database, the database returns an object("pass_to_html"). I want to render the results of that object onto a new page

Show Detail

Django: signal when user logs in?

In my Django app, I need to start running a few periodic background jobs when a user logs in and stop running them when the user logs out, so I am looking for an elegant way to get notified of a u...

Show Detail