---
slug: "count-and-display-django-urls"
title: "Code to Display the Number of URLs in Django"
description: "During an IT audit, it was necessary to represent the scale of the application. To achieve this, I decided to consider the total number of URLs in Django as an indicator of the application's scale."
url: "https://www.ytyng.com/en/blog/count-and-display-django-urls"
publish_date: "2022-11-02T12:34:22Z"
created: "2022-11-02T12:34:22Z"
updated: "2026-02-27T01:09:31.494Z"
categories: ["Django"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20240615/1ba4f277fbf644cf98936e66ef7fb2ec.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Code to Display the Number of URLs in Django

During an IT audit, I needed to represent the size of an application, so I decided to use the total number of URLs in Django to signify the application's scale.

```python
from django.urls.resolvers import get_resolver
root_resolver = get_resolver()


def _join_namespace(a, b):
    return (a + ':' if a else '') + (b or '')


def _count_urls(namespace, resolver):
    count = 0
    _namespace = _join_namespace(namespace, resolver.namespace)
    for ns, (prefix, res) in resolver.namespace_dict.items():
        count += _count_urls(_namespace, res)

    for n, l in resolver.reverse_dict.items():
        if callable(n):
            continue
        count += 1
    return count


print('URLS count:', _count_urls('', root_resolver))
```

This script calculates and prints the total number of URLs in a Django application. By analyzing the URL patterns, it sums up all the available endpoints, thereby providing an indicator of the application's size.
