Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#27569 closed New feature (wontfix)

Resolve reverse url with named groups regular expression to sprintf output formatters not specifying kwargs key / value pairs.

Reported by: Dmitriy Sintsov Owned by: nobody
Component: Core (URLs) Version: 1.10
Severity: Normal Keywords: reverse url
Cc: Triage Stage: Someday/Maybe
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I wrote a pluggable app which supports Django reverse url calling from client-side Javascript (AJAX) via sprintf.js library:

To inject resolved reverse url with regex named parameters replaced to sprintf output formatters, without kwargs key / value pairs supplied, I use Python inspect module when django.django.core.urlresolvers.reverse throws NoReverseMatch exception to get values of caller variables f_locals keys 'prefix_norm' / '_prefix':

Unfortunately that is a very hacky way because the urlresolvers code changes from one Django release to another one and I am afraid that at some point there could be new incompatibility.

Is there any cleaner way to resolve reverse url with regex named parameters replaced to sprintf output formatters not specifying actual kwargs key / value pairs? Because internally urlresolvers module has these as prefix_norm / _prefix values.

Or should I try to develop a core patch? That should be useful to many modules which route Django url names from Javascript client-side.

Change History (3)

comment:1 Changed 6 years ago by Tim Graham

Triage Stage: UnreviewedSomeday/Maybe

A web search for "django reverse urls in javascript" gives django-js-reverse as the first result. I'm not sure if that package is relevant but I think it would be better to discuss the idea on the DevelopersMailingList because it reaches a wider audience than this ticket tracker. It will be useful to know what other solutions are other there. It would also be helpful to describe the problem a high level rather than simply linking to the code you wrote. It takes non-trivial effort to figure out what the use case is by looking at the code, so you're less likely to get useful feedback.

comment:2 Changed 6 years ago by Dmitriy Sintsov

Resolution: fixed
Status: newclosed

Thank you for providing the link to django-js-reverse pluggable app, now I was able to fix hackish way of reverse urls with regex named parameters into sprintf formatters via much cleaner and more compatible way:

The only thing I am unsure about is how the code is valid to different namespaces, however I know that namespaces are rarely used in Django urls.

You asked why do I need this - it's because urls with different named parameters (url kwargs) values has to be constructed in Javascript at client-side for various AJAX calls to Django class-based views and I do not want to use hard-coded urls at client-side.

comment:3 Changed 6 years ago by Tim Graham

Resolution: fixedwontfix

(updating the resolution to reflect that fact that no changes were made in Django as a result of this issue)

Note: See TracTickets for help on using tickets.
Back to Top