Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#21988 closed New feature (worksforme)

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

Reported by: cjerdonek 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


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(),
url(r'^(?P<action>change)/confirm/$', ConfirmChangeView.as_view(),

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

  • Cc chris.jerdonek@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 2 years ago by mjtamlyn

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'},
url(r'^change/confirm/$', ConfirmChangeView.as_view(), {'action': 'change'},

comment:3 Changed 2 years ago by mjtamlyn

  • Resolution set to worksforme
  • Status changed from new to closed

comment:4 Changed 2 years ago by cjerdonek

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 2 years ago by cjerdonek (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top