Opened 7 years ago

Closed 18 months ago

#12943 closed Bug (fixed)

Unnamed captures are not passed to views in included URLconfs

Reported by: petri.lehtinen@… Owned by: bpeschier
Component: Core (URLs) Version: 1.1
Severity: Normal Keywords: ams2015
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


### mypkg/
def my_view(request, *args, **kwargs):
    print args
    print kwargs

### mypkg/
urlpatterns = patterns('mypkg.views', (r'^$', 'my_view'))

### otherpkg/
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/
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.

Attachments (1)

0001-Return-unnamed-captures-from-sub-URLconfs-if-there-a.patch (1.4 KB) - added by petri.lehtinen@… 7 years ago.
Return unnamed captures from sub-URLconfs if there are no named captures

Download all attachments as: .zip

Change History (12)

Changed 7 years ago by petri.lehtinen@…

Return unnamed captures from sub-URLconfs if there are no named captures

comment:1 Changed 7 years ago by anonymous

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by russellm

  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by lukeplant

  • Type set to Bug

comment:4 Changed 5 years ago by lukeplant

  • Severity set to Normal

comment:5 Changed 5 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:6 Changed 5 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:7 Changed 3 years ago by aaugustin

  • Component changed from Core (Other) to Core (URLs)

comment:8 Changed 18 months ago by bpeschier

  • Keywords ams2015 added
  • Owner changed from nobody to bpeschier
  • Status changed from new to assigned

I added a PR for this. It will enforce args and kwargs will never be combined, but if you only use unnamed arguments, it will propagate the positional argument.

comment:10 Changed 18 months ago by knbk

  • Needs tests unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:11 Changed 18 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In f0f5212:

Fixed #12943 -- Allowed unnamed arguments to be propagated in includes

Propagated unnamed arguments as positional arguments into included
URLconfs if no named arguments are defined. Positional and keyword
arguments are never combined.

Note: See TracTickets for help on using tickets.
Back to Top