Opened 15 years ago

Closed 13 years ago

#10840 closed (fixed)

URL fetching for AJAX libraries

Reported by: Mathijs Dumon Owned by: nobody
Component: Generic views Version: dev
Severity: Keywords: ajax url generic fetch
Cc: qingfeng@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When developing AJAX add-ons for my Django apps, they often need to post data on a specific url. Most of the time I'm forced to hard code this url into my scripts. However, if it would be possible to have a single url (read: view) that fetches the url for a given view name + variables, or returns an array of the sort: => "/user_%(id)/...", ... (the %(id) part should than be replaced with the correct parameters), AJAX scripts can become more url-abstract.

My request is a generic view that works as explained above, you could then enable it by adding one more URL to your url.py file pointing to this generic view. Comparable to what we have for i18n for AJAX. Some generic javascript code would also be needed to handle the parsing of the array (using caching etc).

If you are interested in including such a feauture in trunk, I'd even be willing to write up some code.

regards,
Mathijs

Change History (6)

comment:1 by anonymous, 15 years ago

Triage Stage: UnreviewedDesign decision needed

comment:2 by QingFeng, 15 years ago

Cc: qingfeng@… added

Look here :)
Django Ajax Tag, ROR link_to_remote clone.

{% ajax %}
link_to_remote 
url => /demo/ajax1/
update => #ajax1
success => $("#ajax1").css("color","red")
{% endajax %}

http://github.com/qingfeng/django-ajax-tag/tree/master
http://github.com/qingfeng/django-ajax-tag/blob/bc8bc075fecc1799bea99ec18bc03c0fef282010/demo/templates/demo.html

comment:3 by Russell Keith-Magee, 15 years ago

milestone: 1.2

I'm having difficulty understanding what you're proposing here. Could you clarify exactly what it is that this view is for, and what it will acheive? Providing some sample code to demonstrate how it would be used would be a good start.

in reply to:  2 comment:4 by Mathijs Dumon, 15 years ago

@qingfeng, you've missed my point here, you still hard-coding the url /demo/ajax1/ in your template, plus your tag results in in-line javascript tags, which I personnaly loath, plus it is far from toolkit-independent.
@russellm:
What I'm talking about is a view returning a JSON object looking something like this:
{

"named_javascript_view1": "/url_to/named_view1/",
"named_javascript_view2": "/url_to/named_view2/"

}

There are however two use-cases to think about. We can have static urls (no parameters in it) and dynamic urls. You can load the static urls once and cache them in an array (e.g. viewsnamed_javascript_view1), the dynamic ones can either be fetched on-demand, or we can use regular expressions in the Javascript file. The last option seems better, as long as there's no problem with security.

This generic view would thus load the urls file, parse it and send all named views as a JSON response. The achievement would be that AJAX requests no longer need hard-coded urls.

It would be similar to the i18n extensions because you would still need to include the url yourself in the urls file. This implementation does not require changes to the existing code base, and is as such completely modular, in accordance with Django's philosophy.

comment:5 by mlouro, 14 years ago

I don't think a generic view is ideal for this, since it would require one ajax call to the server to retrieve urls. I have built a simple command (dump_urls) that outputs a JS Object Literal with view names and urls that I think is a good start for something like this, and a better approach design wise. It lives @ http://github.com/mlouro/django-extensions for now

Example output:

{

"task_ajax_complete": "/projects/<project_id>/task/ajax/complete/<task_id>/",
"task_ajax_remove": "/projects/<project_id>/task/ajax/remove_task/",
"task_delete": "/projects/<project_id>/task/delete/<task_id>/",
"task_detail": "/projects/<project_id>/task/<task_id>/",
"task_edit": "/projects/<project_id>/task/save/<task_id>/",
"task_index": "/projects/<project_id>/task/",
"time_add": "/projects/<project_id>/time/save/",
"time_delete": "/projects/<project_id>/time/delete/<time_id>/",
"time_edit": "/projects/<project_id>/time/save/<time_id>/",
"time_index": "/projects/<project_id>/time/"

}

comment:6 by Russell Keith-Magee, 13 years ago

Resolution: fixed
Status: newclosed

I'm fairly certain this is now possible given the introduction of class-based views in [14254].

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