Skip to content

Instantly share code, notes, and snippets.

@haridutt12
Created April 11, 2019 05:34
Show Gist options
  • Save haridutt12/86363f57655caaaf30a4a8d2049a32b2 to your computer and use it in GitHub Desktop.
Save haridutt12/86363f57655caaaf30a4a8d2049a32b2 to your computer and use it in GitHub Desktop.
def user_login(request):
if request.method == 'POST':
# First get the username and password supplied
username = request.POST.get('username')
password = request.POST.get('password')
# Django's built-in authentication function:
user = authenticate(username=username, password=password)
# If we have a user
if user:
#Check it the account is active
if user.is_active:
# Log the user in.
login(request,user)
# Send the user back to some page.
# In this case their homepage.
return HttpResponseRedirect(reverse('index'))
else:
# If account is not active:
return HttpResponse("Your account is not active.")
else:
print("Someone tried to login and failed.")
print("They used username: {} and password: {}".format(username,password))
return HttpResponse("Invalid login details supplied.")
else:
#Nothing has been provided for username or password.
return render(request, 'basic_app/login.html', {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment