Python Django
magic links

How to add magic links to a Python Django application

FusionAuth provides a convenient and quick way to implement magic links in your Python Django app

How-To > Python Django > Magic Links

Start implementing magic links by setting up FusionAuth

Magic links are a passwordless method of authentication and make it easier for your users to log in. They work by sending the user a one time code in a link, typically as an email or SMS message. When the user clicks on the link, they are then logged in to the application.

In this doc, you’ll learn how to add magic links functionality to your Python Django application. You’ll use FusionAuth to easily add magic links.

1. Configure FusionAuth

First, set up FusionAuth and a sample Python Django application. Please follow this tutorial to get that sample application set up.

All done? Great.

Let’s get magic links set up for your Python Django application.

Now, let’s enable magic links using FusionAuth. Create another FusionAuth script. To set up magic links, all you need to do is tweak FusionAuth configuration.

Go back to the fusionauth-setup directory:

cd ../setup-fusionauth

You can use the same client library and API key as you used when setting up FusionAuth and the sample Python Django application. But here, you are configuring a different aspect, namely magic links.

Put the below code in add-magic-link.py.

from fusionauth.fusionauth_client import FusionAuthClient import os import sys APPLICATION_ID = "e9fdb985-9173-4e01-9d73-ac2d60d1dc8e"; # You must supply your API key api_key_name = 'fusionauth_api_key' api_key = os.getenv(api_key_name) if not api_key: sys.exit("please set api key in the '" + api_key_name + "' environment variable") client = FusionAuthClient(api_key, 'http://localhost:9011') client_response = client.patch_application(APPLICATION_ID, {"application": {"passwordlessConfiguration":{"enabled":True}}}) if not client_response.was_successful(): sys.exit("couldn't update application "+ str(client_response.error_response))

Run this code:

fusionauth_api_key=<your API key> python add-magic-link.py

You are all done! With this configuration code, you’ve enabled magic links for your Python Django application.

You can also configure magic links manually by logging into the FusionAuth admin user interface and navigating to applications, then the application configuration, then the security tab, then the passwordless login section.

Manually setting up magic links.
Manually setting up magic links.

Now, test to see that you have magic links enabled for your application. Close any other incognito windows you have open and open a new one. (This ensures you’ve logged out of the application.)

Visit the Python Django application and log in again.

Now, on the login page, you should see a magic link button.

What the user sees with magic links enabled.
What the user sees with magic links enabled.

This is the default look and feel, but you can configure branding using themes.

If you want to actually get an email and have log in, make sure you set up your FusionAuth email configuration using a service like Sendgrid or Mailgun.

Learn more by visiting the FusionAuth developer docs

Python Django How-Tos

Magic Links How-Tos