Nifty Django Feature: resolve function
Title: Nifty Django Feature: resolve function
URL Source: https://www.better-simple.com/django/2026/07/08/nifty-feature-resolve-function/
Published Time: 2026-07-08T00:00:00+00:00
Markdown Content:
A nifty feature in Django is the resolve function. This is how Django determines what view should be called and with what arguments. It allows you to parse the relative path on a request and get the view, view arguments and the URL name, among other things.
For the given view and urls:
```
views.py~ish
from django.http import HttpResponse
def pet_detail(request, pet_id): return HttpResponse(f"Pet ID={pet_id}")
def pet_list(request): return HttpResponse("The pets!")
urls.py~ish
from django.urls import path
urlpatterns = [ path("pets//", pet_detail, name="pet_detail"), path("pets/", pet_list, name="pet_list"), ] ```
We can do the following:
``` from django.urls import resolve
match = resolve("/pets/42/")
print(match.func) # print(match.kwargs) # {'pet_id': 42} print(match.url_name) # pet_detail ```
There are actually several properties on the returned ResolverMatch object that are useful, I only highlighted the obviously cool ones. It’s worth reviewing Django’s reference documentation to see what else is there.
Using resolve to build an analytics tool
Bear with me a moment and allow me to set the scene.
Let’s say you’ve built a small web app that only contains what you need it to. It’s bare bones, but functional. People like it and have been using it and recommending it to others. Eventually your app stabilizes and becomes a bit more rigid. Things can’t change as quickly as they have in the past. It takes longer to make changes, so there needs to be more justification than “I think this feature would be cool.” Especially challenging is determining what can be removed or changed without significant impact.
What’s required here is more information. Specifically information on how users are using the web app. If you haven’t built in this type of analytics from the start, you’ll have to instrument your application and then wait several months for data to collect.
But wait!
Don’t forget about your logs. They tell you the story of how users use your application. They just need to be parsed out into the shapes of your application. This is where resolve comes in.
You can use resolve to parse your web server logs to create an analytics solution that tells you how users have been using your application and more.
So now these logs:
[2026-05-26T14:32:01+00:00] "GET /pets/42/ HTTP/1.1" 200 1024
[2026-05-26T14:32:45+00:00] "GET /pets/ HTTP/1.1" 200 4096
[2026-05-26T14:33:12+00:00] "POST /pets/7/ HTTP/1.1" 302 0
Magically get turned into a table that looks like:
| timestamp | view | pet_id | | --- | --- | --- | | 2026-05-26T14:32:01+00:00 | pet_detail | 42 | | 2026-05-26T14:32:45+00:00 | pet_list | | | 2026-05-26T14:33:12+00:00 | pet_detail | 7 |
From here, you can build out a new internal tool that helps people view usage analytics of your web app from as far back as you have web server logs. This avoids having to instrument the application and then wait. Instead, you get instant gratification. If you do go this route, be aware that your logs will contain requests against old versions of your URLs. So you’ll need to know when your urlpatterns changed over time to be able to parse all of your logs.
So give it a whirl and see how your app is being used.
If you have thoughts, comments, or questions, please let me know. You can set up a meeting with me, or find me on the Fediverse, Django Discord server or use email.