Code

Opened 5 years ago

Closed 4 years ago

#10840 closed (fixed)

URL fetching for AJAX libraries

Reported by: mathijs Owned by: nobody
Component: Generic views Version: master
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: UI/UX:

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

Attachments (0)

Change History (6)

comment:1 Changed 5 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 follow-up: Changed 5 years ago by qingfeng

  • 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 Changed 5 years ago by russellm

  • milestone 1.2 deleted

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.

comment:4 in reply to: ↑ 2 Changed 5 years ago by mathijs

@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 Changed 5 years ago by mlouro

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 Changed 4 years ago by russellm

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

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.