About CSRF Cookie on Any Page

By default, CSRF cookie is only passed to the Django views which render CSRF token. To ensure that it is used on any specific page, for instance, a template view that has some forms rendered by JavaScript, use the @ensure_csrf_cookie decorator:

from django.shortcuts import render
from django.views.decorators.csrf import ensure_csrf_cookie

@ensure_csrf_cookie
def index(request):
    return render(request, "index.html")

Tips and Tricks Programming Development Security Django 5.2 Django 4.2 Cookies