Unnamed captures are not passed to views in included URLconfs
|Reported by:||petri.lehtinen@…||Owned by:||bpeschier|
|Cc:||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
### mypkg/views.py: def my_view(request, *args, **kwargs): print args print kwargs ### mypkg/urls.py: urlpatterns = patterns('mypkg.views', (r'^$', 'my_view')) ### otherpkg/urls.py: urlpatterns = patterns('', (r'^(\w+)/', include('mypkg.urls'))
With this setup, in my_view(), both args and kwargs are empty, even though there are no named captures that would make Django ignore the unnamed ones.
The following works, as the capture in the upper URLconf is named:
### otherpkg/urls.py: urlpatterns = patterns('', (r'^(?P<foo>\w+)/', include('mypkg.urls'))
In this case, foo is passed as a kwarg to my_view().
The attached patch fixes this, but there's still an issue with extra kwargs to views: As the URL resolving returns triples of the form (view_func, view_args, view_kwargs) along from sub URLconfs back to the including URLconf, there's no way of knowing whether we had named captures or extra kwargs in the sub URLconf. They are all collected to view_kwargs which is then returned.
This could be fixed by passing around quadruples (view_func, view_args, view_kwargs, extra_kwargs), and collect all named URL captures along the chain to view_kwargs and all extra kwargs to extra_kwargs. If at some level the view_kwargs is not empty, we can throw away all the unnamed captures, i.e. set view_args to (). This fix would be a bit more intrusive, though, so I haven't tried to do it yet.
Change History (12)
Changed 6 years ago by petri.lehtinen@…
comment:1 Changed 6 years ago by anonymous
- Has patch set
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 6 years ago by russellm
- Needs tests set
- Triage Stage changed from Unreviewed to Accepted
comment:8 Changed 9 months ago by bpeschier
- Keywords ams2015 added
- Owner changed from nobody to bpeschier
- Status changed from new to assigned
comment:10 Changed 9 months ago by knbk
- Needs tests unset
- Triage Stage changed from Accepted to Ready for checkin