On 03/17/2011 10:44 AM, Marco Louro wrote:
> I don't really know how to approach this issue the best way, so I'm
> just going to be direct, I'd love to see Django provide support for
> django.core.urlresolvers.reverse (at least) on the client (in
> JavaScript).
I think this is really interesting, and I'd like to play with your
approach. It does seem to me like something that, while quite useful for
some projects, not all projects will need, and that it ought to stand on
its own as a reusable app for quite a while to prove its usefulness and
shake out any issues (and probably just stay that way).
Just to give you a point of comparison, database migrations are a
feature that is much more core to Django than this, and even those were
left out of core for years to allow several different approaches to sort
themselves out. Even now that South is the clear winner in that space,
only a portion of South is currently being considered for core inclusion.
I don't immediately see anything that would prevent implementing your
proposal as a reusable app outside core. If you do run into problems
where a small change in core could make your life a lot easier in
implementing this as a reusable app, please do bring it up here or file
a bug.
Carl
I would be more then interested in contributing to such app. I think
it would be a good addition to i18n JS library that Django provides.
Right now, I'm using my own view that generates a dictionary from all
*named* views and provides a simple reverse() with syntax similar to
yours. My biggest issues right now are a) efficient caching b) making
it work well with asset managers. Maybe I'm missing something, but
last time I checked staticfiles didn't had an option to include
dynamically generated content.
--
Łukasz Rekucki
Having run into this issue one too many times, I would like to raise
my voice to say that I am *very much* interested in having this
included in Django.
Ayaz
This will store my own, extendable configuration into an project
specific variable (javascript object), which I can easily access
(config.foo.bar). It can be used to store URLs, but may also contain
generic things like MEDIA_URL.
So far it only solves one problem, getting variables into JS in an sane,
structured way...it doesn't solve handling URLs which accept parameters.
URL that don't need parameters are easy to, resolve() does all the work.
Parameters get tricky, but I have come up with an easy and efficient
solution, although it does look kind of strange. I use resolve() to get
a valid URL, but put some placeholder into it, which means:
resolve('my_named_view', kwargs={'slug': '__slug__'})
On the JS-side creating the final url just needs a simple string
replacement. Of course the URL needs to be valid, which means it may not
fit all situations (numeric ids: '(?P<pk>[0-9]+|__pk__)'?). Anyways this
works fine for me, while still being extenable enough. In addition it
solves the more generic problem of putting variables into JS, which is
kind of nice I think.
The resulting JS could be cached using normal template caching tags, or
may be moved into its own HTTP request if wanted. The idea of using some
kind of context processors for JS-context is what this email is
about. ;-)
David
This all sounds pretty cool - I'd love to be able to reverse URLs in
JavaScript. Keep in mind, though, that URLConfs can change on a
per-request basis, by setting request.urlconf. Any JSON data structure
representing the URLConf would need to be generated after this has been
set (ie. some time after request middleware has been processed, look at
django.core.handlers.base:94-98). Having a template tag would probably
be fine, as the URLConf is set by render time, but don't expect to be
able to do a one-off analysis of the URLConf at application startup.
Cheers,
Dan
--
Dan Fairs | dan....@gmail.com | www.fezconsulting.com
+1