In the vast ecosystem of Django, there exists an almost endless set of libraries designed to extend its functionality, catering to the diverse needs of developers.

Among these, Django Sesame stands out as a robust tool specifically designed to streamline the process of implementing token-based authentication in Django applications.

This article delves into the core features, benefits, and use cases of Django Sesame, shedding light on why it is an indispensable tool for developers looking to enhance their Django projects with secure and user-friendly authentication mechanisms.

Introduction to Django Sesame

Django Sesame is a third-party library that offers a simple yet powerful solution for token-based authentication in Django projects.

It operates by generating a unique, secure token for each user, which can then be used to authenticate requests without the need for traditional login forms or session-based authentication methods.

This approach not only simplifies the authentication process but also enhances security by eliminating the need for storing sensitive session data on the server.

Core Features of Django Sesame

Token Generation and Validation: Django Sesame provides utilities to generate and validate authentication tokens. These tokens are tied to the user's identity and can be customized to expire after a certain period, adding an extra layer of security.

Seamless Integration with Django's Authentication System: It seamlessly integrates with Django's built-in authentication system, making it easy to implement in existing projects without significant modifications.

Email-Based Login Links: One of the standout features of Django Sesame is its support for sending email-based login links. Users can log in by simply clicking a link, removing the need for remembering passwords.

Customizable Token Expiry: Developers have the flexibility to define the lifespan of the authentication tokens, allowing for a balance between convenience and security based on the application's requirements.

Benefits of Using Django Sesame

Enhanced Security: By leveraging token-based authentication, Django Sesame minimizes the risk associated with traditional authentication methods, such as password theft or session hijacking.

Improved User Experience: The ability to authenticate via email links or tokens simplifies the login process, offering a more streamlined user experience, especially for applications that require temporary access or single sign-on scenarios.

Ease of Implementation: Django Sesame is designed to be easy to integrate into existing Django projects, providing a straightforward solution for developers looking to implement token-based authentication without extensive modifications to their codebase.

Use Cases

Django Sesame is particularly well-suited for applications where traditional session-based authentication is not ideal. Some common use cases include:

  • Single Sign-On (SSO) Systems: For applications that serve as part of a larger ecosystem, Django Sesame can facilitate SSO by allowing users to authenticate once and gain access to multiple services.
  • Email-Based Authentication: Applications that prefer to authenticate users through email links for ease of access or to provide a passwordless login experience.
  • API Authentication: For Django applications that serve as backends for front-end frameworks or mobile applications, Django Sesame can provide a secure and efficient means of authenticating API requests.

Getting Started with Django Sesame

To get started with Django Sesame for authentication via magic links in a Django project, you initially need to install Django Sesame:

pip install django-sesame

Following that, you must configure your Django project's settings to include Sesame's authentication backend:


Then, incorporate LoginView into your URL configuration to streamline the login process:

from django.urls import path
from sesame.views import LoginView

urlpatterns = [
    path("sesame/login/", LoginView.as_view(), name="sesame-login"),

Using Django Sesame

Below is an example of how you can create a Django view that includes a form for submitting an email address.

Upon form submission, it generates a "Magic Link" using Django Sesame and sends this link to the provided email address.

First the form definition:

from django import forms

class EmailForm(forms.Form):
    email = forms.EmailField()

Then the view:

from django.core.mail import send_mail
from django.contrib.auth import get_user_model
from django.shortcuts import render, redirect
from django.urls import reverse
from sesame.utils import get_query_string

from .forms import EmailForm

def send_magic_link_view(request):
    if request.method == 'POST':
        form = EmailForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            User = get_user_model()
                user = User.objects.get(email=email)
                login_url = request.build_absolute_uri(reverse('sesame-login'))
                magic_link = login_url + get_query_string(user)
                    'Your Magic Login Link',
                    f'Use this link to login: {magic_link}',
                # Redirect to a new URL or inform the user to check their email
                return redirect('success_page')
            except User.DoesNotExist:
                # Handle case where no user exists for the provided email
        form = EmailForm()

    return render(request, 'your_template_name.html', {'form': form})

Remember to replace '', and 'your_template_name.html' with your actual from email address, and template name, respectively.

When the form is submitted with a valid email, it looks up the user by that email, generates a magic link using Django Sesame, and sends an email to the user with the link.


Django Sesame offers a compelling solution for developers looking to incorporate token-based authentication into their Django applications.

Its ease of use, coupled with the significant security and user experience benefits, makes it an attractive choice for a wide range of projects.

Whether you're building a new application or seeking to enhance an existing one, Django Sesame provides the tools necessary to implement efficient, secure, and user-friendly authentication mechanisms.

Thank you for reading and I will see you on the Internet.

This post is public so feel free to share it.

If you like my free articles and would want to support my work, consider buying me a coffee:

Are you working on a project that’s encountering obstacles, or are you envisioning the next groundbreaking web application?

If Python, Django, and AI are the tools you're exploring but you need more in-depth knowledge, you're in the right place!

Get in touch for a 1-hour consultation where I can address your specific challenges.

Developer Service Blog - 1 Hour Consulting - Nuno Bispo
Are you working on a project that’s hitting roadblocks or simply dreaming up the next big web application?

Tagged in: