Django URLpatterns无法捕获URL变量,访问带参路由报404
Hey there, let's dig into why your URL variable capture isn't working as expected—this is a super common gotcha with Django's urlpatterns, so we'll get it sorted out quickly!
First, Check the Order of Your URL Patterns
Django matches URL patterns top to bottom in the urlpatterns list. If you have a fixed path like path('views/productview/', ...) listed before your parameterized pattern, Django will match that fixed path first when you visit /views/productview/1/, leading to a 404 because it never gets to the pattern that captures the ID.
Fix: Move your parameterized URL pattern above any fixed paths that start with the same prefix. For example:
from django.urls import path from . import views urlpatterns = [ path('about/', views.about_view, name='about'), path('views/available_products/', views.available_products, name='available_products'), path('admin/', admin.site.urls), # Parameterized pattern comes first path('views/productview/<int:product_id>/', views.productview, name='product_detail'), # Fixed path comes after path('views/productview/', views.product_list, name='product_list'), ]
Verify Your Path Converter/Regex Syntax
If you're using path() (the preferred method for most cases), make sure you're using the correct converter syntax: <converter_type:variable_name>. For integer IDs, that's <int:product_id> (or whatever variable name you want).
If you're using re_path() (for regex patterns), double-check that your regex properly captures the ID and anchors the pattern with ^ and $ to avoid partial matches:
from django.urls import re_path urlpatterns = [ # Correct regex for capturing an integer ID re_path(r'^views/productview/(?P<product_id>\d+)/$', views.productview, name='product_detail'), ]
Missing the ^ (start of string) or $ (end of string) can cause Django to match unintended parts of the URL, leading to 404s.
Ensure Your View Function Accepts the Correct Parameter
The "missing parameter" error when visiting /views/productview// tells us that Django is matching the parameterized pattern, but your view isn't set up to receive the variable.
Check two things:
- The variable name in your URL pattern matches exactly what's in your view. For example, if your URL uses
<int:product_id>, your view should look like this:def productview(request, product_id): # Your logic here (e.g., fetch product by product_id) return HttpResponse(f"Showing product {product_id}") - You haven't accidentally omitted the parameter from the view definition—that's the most common cause of the "missing parameter" error.
Quick Check: APPEND_SLASH Setting
By default, Django's APPEND_SLASH setting is True, which automatically redirects URLs without a trailing slash to the version with one. Make sure your URL patterns include the trailing slash (like /views/productview/<int:product_id>/) to match the URLs users are visiting. If your pattern doesn't have a trailing slash but users are adding one, you'll get a 404 unless APPEND_SLASH handles the redirect.
内容的提问来源于stack exchange,提问作者Grak

