Performance: URL parameter type converters
This affects the server-side URL routing speed and the initial response time impacting page load speed.
path('item/<int:item_id>/', views.item_detail)path('item/<str:item_id>/', views.item_detail)| Pattern | CPU Overhead | Type Checking | Response Time Impact | Verdict |
|---|---|---|---|---|
| Generic <str> converter | Higher | More checks | Slightly slower | [X] Bad |
| Specific <int> converter | Lower | Fewer checks | Faster | [OK] Good |
int converter do in a Django URL pattern?int converterint converter matches only digits and converts them to Python integers.post_slug in a Django URL pattern?slug converter matches letters, numbers, underscores, and hyphens.<slug:post_slug> inside the path string.path('user//', views.profile) and the URL /user/42/, what will be the type and value of user_id in the view?int converter converts the URL part to an integer.int converter and becomes integer 42.path('article//', views.detail) when the URL /article/abc/ is requested.int converter only matches digits; 'abc' is not digits.int, Django returns a 404 error (no match).str converter stops at slashes, so it cannot capture paths with slashes.path converter matches any string including slashes, perfect for file paths.path converter because it matches any string including slashes. -> Option APass the test and this pattern moves unknown