About URL Resolving

Using the resolve_url() function, your third-party Django app can let users define various types of URLs via settings—for example, an EASTER_EGG_URL setting:

from django.shortcuts import resolve_url, render, redirect
from django.conf import settings

def boring_info(request):
    if "secret-game" in request.GET:
        return redirect(resolve_url(settings.EASTER_EGG_URL))
    return render(request, "misc/boring_info.html")

The resolve_url() function accepts the following types of input:

  • an object that has a get_absolute_url() method;
  • a relative path (e.g. "./change/");
  • a named URL path (e.g. "accounts:register");
  • a lazily reversed URL path (e.g. reverse_lazy("accounts:register");
  • a full URL (e.g. "https://djangotricks.com").

Tips and Tricks Programming Django 5.2 Django 4.2 Django 3.2