Opened 7 years ago

Closed 2 years ago

#12943 closed Bug (fixed)

Unnamed captures are not passed to views in included URLconfs

Reported by: petri.lehtinen@… Owned by: Bas Peschier
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

comment:2 Changed 7 years ago by Russell Keith-Magee

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:3 Changed 6 years ago by Luke Plant

Type: Bug

comment:4 Changed 6 years ago by Luke Plant

Severity: Normal

comment:5 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:7 Changed 4 years ago by Aymeric Augustin

Component: Core (Other)Core (URLs)

comment:8 Changed 2 years ago by Bas Peschier

Keywords: ams2015 added
Owner: changed from nobody to Bas Peschier
Status: newassigned

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 2 years ago by Marten Kenbeek

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:11 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

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