Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#21988 closed New feature (worksforme)

reverse() shouldn't require kwargs for uniquely determined named groups

Reported by: Chris Jerdonek Owned by: nobody
Component: Core (URLs) Version: 1.6
Severity: Normal Keywords:
Cc: chris.jerdonek@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For DRY purposes, it can be useful to capture a known substring in an URL for access from a view. For example--

url(r'^(?P<action>create)/confirm/$', ConfirmCreateView.as_view(),
    name='confirm_create')
url(r'^(?P<action>change)/confirm/$', ConfirmChangeView.as_view(),
    name='confirm_change')

Calling reverse() for examples like the above still requires passing the keyword argument though (action='create' or action='change' in these examples), even though the value of the keyword argument isn't necessary to construct the corresponding URL. This makes calling reverse() less DRY than it needs to be.

I would like to suggest making reverse() not require the keyword arguments in cases where the corresponding named groups have only one possible value. If this isn't possible to add, are there any suggestions for a DRY way to accomplish the same thing?

Change History (4)

comment:1 Changed 3 years ago by Chris Jerdonek

Cc: chris.jerdonek@… added

comment:2 Changed 3 years ago by Marc Tamlyn

I don't think it would be possible (or reasonable) to detect this pattern out of the regex. However, there is already a way to achieve this without the DRY issues:

url(r'^create/confirm/$', ConfirmCreateView.as_view(), {'action': 'create'},
    name='confirm_create')
url(r'^change/confirm/$', ConfirmChangeView.as_view(), {'action': 'change'},
    name='confirm_change')

comment:3 Changed 3 years ago by Marc Tamlyn

Resolution: worksforme
Status: newclosed

comment:4 Changed 3 years ago by Chris Jerdonek

Thanks for the suggestion, @mjtamlyn. It's not quite as DRY as the proposed feature (there is an extra occurrence of the action name), but it is a good solution.

Last edited 3 years ago by Chris Jerdonek (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top