Opened 8 years ago

Closed 8 years ago

Last modified 8 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

Description

I wrote a pluggable app which supports Django reverse url calling from client-side Javascript (AJAX) via sprintf.js library:
https://github.com/Dmitri-Sintsov/django-jinja-knockout/blob/e28d83886c5da0f4661257146a9925aabbcc93b8/django_jinja_knockout/static/js/front/app.js#L1276

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':
https://github.com/Dmitri-Sintsov/django-jinja-knockout/blob/e28d83886c5da0f4661257146a9925aabbcc93b8/django_jinja_knockout/tpl.py#L180

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 by Tim Graham, 8 years ago

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 by Dmitriy Sintsov, 8 years ago

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:

https://github.com/Dmitri-Sintsov/django-jinja-knockout/commit/ac39fd0f3f37bb8ef5ce85a571a537ed3708b21d

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 by Tim Graham, 8 years ago

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