About URL Resolving

Using the resolve_url() function, you can allow your Django app users to set various types of URLs in the settings. For example, EASTER_EGG_URL setting:

1
2
3
4
5
6
from django.shortcuts import resolve_url, render, redirect

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() accepts 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"), or a full URL (e.g. "https://djangotricks.com").

Tips and Tricks Programming Django 5.x Django 4.2 Django 3.2