Opened 14 years ago

Closed 9 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

Description

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

Attachments (1)

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

Download all attachments as: .zip

Change History (12)

by petri.lehtinen@…, 14 years ago

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

comment:1 by anonymous, 14 years ago

Has patch: set

comment:2 by Russell Keith-Magee, 14 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:3 by Luke Plant, 13 years ago

Type: Bug

comment:4 by Luke Plant, 13 years ago

Severity: Normal

comment:5 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:7 by Aymeric Augustin, 11 years ago

Component: Core (Other)Core (URLs)

comment:8 by Bas Peschier, 9 years ago

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

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:11 by Tim Graham <timograham@…>, 9 years ago

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