Opened 19 months ago

Closed 19 months ago

Last modified 19 months 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

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 19 months ago by cjerdonek

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

comment:2 Changed 19 months 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'},
    name='confirm_create')
url(r'^change/confirm/$', ConfirmChangeView.as_view(), {'action': 'change'},
    name='confirm_change')

comment:3 Changed 19 months ago by mjtamlyn

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

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