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