DjangoCon US 2024: path('/user/user.username:user/', view_profile) with Willem Van Onsem

Channel:
Subscribers:
42,400
Published on ● Video Link: https://www.youtube.com/watch?v=CMap3w-ZHeY



Duration: 0:00
47 views
0


Since Django-2.0, most people use path converters instead of regexes to describe the different URL patterns, and how these will trigger views. One can use the already builtin path converters, but also define new ones to parse dates, booleans, etc. more effectively. One can not only define a pattern, but also provide methods to convert between Python objects and URL fragments.

That last feature can be used to automatically fetch model objects if the path contains the value for the primary key of the model object, or another unique field. We thus avoid writing queries to fetch the objects, but can also turn these into lazy objects where we postpone the roundtrip to the database, until the view needs that object, and thus save a roundtrip if the view has a codepath that does not require the object. Some databases also allow to combine multiple queries in one roundtrip, so we can combine all the objects that go to the same database, limiting the roundtrips further.

A problem with path patterns in general is that often they overlap: the same path can trigger multiple patterns. In that case the first one is picked. This issue has already lead to countless hours of debugging since people expect a different view to be triggered. Since path patterns eventually compile to regexes, and one can determine if two regexes (fully) overlap, we can automatically detect if there are paths for which two or more URL patterns are triggered, and in case a pattern is fully covered by patterns above, advise to rearrange the patterns. Probably not very surprisingly we found that Django's admin pages suffer from this issue as well: if we make a model with a CharField as primary key, then adding items with "remove" or "history" as value for the primary key, means certain views can not be used for these objects. We show some ways to mitigate this.


This talk was presented at: https://2024.djangocon.us/talks/path-...

LINKS:
Follow Willem Van Onsem 👇
Website: https://www.django-antipatterns.com/

Follow DjangoCon US 👇
https://fosstodon.org/@djangocon
https://x.com/djangocon

Follow DEFNA 👇
https://www.defna.org/


Video Production by Confreaks
Follow Confreaks 👇
https://confreaks.com/
https://x.com/confreaks
https://confreaks.bsky.social/




Other Videos By Confreaks


2025-03-21DjangoCon US 2024: Only reliable Data: Protecting Database Integrity with Eva Nanyonga
2025-03-21DjangoCon US 2024: Lessons from E2E Testing Web Applications with Avindra Fernando
2025-03-20DjangoCon US 2024: Optimizing remote work: Pull Requests, Stand-ups, and emojis with Kasey Kelly
2025-03-20DjangoCon US 2024: Pygoat - Learn django security the hard way with Adarsh Divakaran
2025-03-19DjangoCon US 2024: Maps with Django with Paolo Melchiorre
2025-03-19DjangoCon US 2024: PostgreSQL Beyond Django: Strategies to Get Max Performance with Álvaro Justen
2025-03-18DjangoCon US 2024: Open-source Python tools to visualize and analyse ... with Samweli Mwakisambwe
2025-03-18DjangoCon US 2024: One Thousand and One Django Sites with Vince Salvino
2025-03-17DjangoCon US 2024: Passkeys: Your password-free future with Ryan Hiebert
2025-03-17DjangoCon US 2024: Django Ledger | Uniting Developers and Accountants for... with Miguel Sanda
2025-03-16DjangoCon US 2024: path('/user/user.username:user/', view_profile) with Willem Van Onsem
2025-03-16DjangoCon US 2024: An Opinionated Guide to Modern Django Forms with Josh Thomas
2025-03-14DjangoCon US 2024: API Maybe: Bootstrapping a Web Application circa 2024 with Carlton Gibson
2025-03-14DjangoCon US 2024: Panel Discussion: Open, friendly, and welcoming: on the... with Lacey Henschel
2025-03-13DjangoCon US 2024: Django User Model: Past, Present, and Future with Will Vincent
2025-03-13DjangoCon US 2024: A Guided Tour Through Postgres Internals with Elizabeth Garrett Christensen
2025-03-12DjangoCon US 2024: WebRTC with Django, Channels, HTMX, and coturn with Ken Whitesell
2025-03-12DjangoCon US 2024: A Brief History of Django with Frank Wiles
2025-03-12DjangoCon US 2024: Keynote - The Fellowship of the Pony with Natalia Bidart
2025-03-11DjangoCon US 2024: Fighting Homelessness with Django with Benjamin "Zags" Zagorsky
2025-03-11DjangoCon US 2024: Hidden gems of Django 5.x with Sarah Boyce