在AJAX调用使用Django的URL标签 [英] Using the Django URL Tag in an AJAX Call

查看:797
本文介绍了在AJAX调用使用Django的URL标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有岗位的地段和讨论利用Django和AJAX的页面,我读过数百名在过去一天左右寻找这个问题的答案。快速预览:

的例子可以表明这样的硬codeD网址:

  $岗位(/项目/新建/,{名:名},功能(数据){...
 

或者一些使用URL模板标签,但不带任何参数:

  $后({%URL create_project%},{名:名},功能(数据){...
 

不过,我想包括在URL一个Django风格的参数。这里是我的网址定义:

<$p$p><$c$c>url(r'ajax/entity_name/(?P<pk>\w+)/$',EntityAjaxView.as_view(),name='entity_name'),

是的,我使用的是基于类的视图,它基于的DetailView。这个观点看起来默认情况下,在URL中提供一个PK值,并在一个正常的模板,我会用:

  {%URL ENTITY_NAME ID_NUMBER%}
 

要提供一个链接。在我的code,我要抢在输入框中输入的PK值的值。这里是我的JavaScript的代码段(不工作):

  VAR ID_NUMBER = $('#id_endowmententity_set-'+ ROWNUM +-id_number)。VAL()
$阿贾克斯({
    键入:GET,
网址:'{%URL ENTITY_NAME ID_NUMBER%}',
 

所以,我的问题是,我可以使用URL模板标记一个值从一个输入框?

(我知道我可以使用POST而不是GET,并通过ID_NUMBER在POST数据,但不会用的DetailView工作。)

在此先感谢您的帮助。

解决方案

Django是一个服务器端应用程序。 JavaScript是客户端。 Django模板得到呈现在服务器上,因此 {%URL ENTITY_NAME ID_NUMBER%} 是在服务器端进行评估,然后它的值返回给客户端。正因为如此,它是不可能为你Django模板结合的JavaScript。然而,有两件事情可以做,以解决您的问题。

在<$ C $

既然你正在Ajax调用,并在Ajax调用依赖于一些用户输入,通常是客户的最佳路线发送任何类型的用户输入到服务器是通过或者使用查询字符串(事C>?在的网址),或通过发送 POST 数据。所以,最简单的事情是改变你的网址不包括 PK 的URL,但对于以获取为 GET <部分/ code>或 POST 数据。

  URL(r'ajax / ENTITY_NAME / $',EntityAjaxView.as_view(),名称='ENTITY_NAME'),
 

和视图(对不起,我不熟悉的类为本次):

 高清ENTITY_NAME(要求):
    PK = request.GET.get(PK)
    ...
 

这对我来说是最完美的解决方案。然而,如果你确实需要建立在客户端的URL,就可以生成一个模板URL在服务器端,然后更换任何零件,你需要在客户端,以获得完整的URL。然而,这需要更多的维护,因此是更容易出错。这种方法的简单的JS例如:

  VAR ID_NUMBER = $('#id_endowmententity_set-'+ ROWNUM +-id_number)。VAL()
    。URL ='{%URL ENTITY_NAME 0%}'替换('0',ID_NUMBER);
$阿贾克斯({
    键入:GET,
    网址:网址,
    ...
});
 

There are LOTS of post and pages discussing the use of Django and AJAX, and I've read hundreds over the past day or so looking for the answer to this question. A quick overview:

May of the examples show a hard-coded URL like this:

$.post("/projects/create/", {"name" : name}, function(data) {...

or some use the URL template tag, but with no parameters:

$.post("{% url create_project %}", {"name" : name}, function(data) {...

However, I'd like to include a Django-style parameter in a URL. Here's my url definition:

url(r'ajax/entity_name/(?P<pk>\w+)/$',EntityAjaxView.as_view(),name='entity_name'),

Yes, I'm using a class based view, and it is based on DetailView. This view looks by default for a pk value to be provided in the URL, and in a normal template I would use:

{% url entity_name id_number %}

to provide a link. In my code, I want to grab the value entered in an input box for the pk value. Here is a snippet of my JavaScript (which doesn't work):

var id_number = $('#id_endowmententity_set-' + rownum + '-id_number').val()
$.ajax({
    type: "GET",
url: '{% url entity_name id_number %}',

So, my question is, can I use the URL template tag with a value from an input box?

(I know that I could use POST instead of GET and pass the id_number in the POST data, but that won't work well with the DetailView.)

Thanks in advance for your help.

解决方案

Django is a server-side application. Javascript is client-side. Django templates get rendered on the server, so {% url entity_name id_number %} is evaluated on the server side, and then it's value is returned to the client. Just because of this, it's impossible for you to combine Django templates with javascript. However there are couple of things you can do to solve your problem.

Since you are making an ajax call, and the ajax call depends on some user input, usually the best route for the client to send any type of user input to the server is by either using querystring (thing after ? in the URL) or by sending a POST data. So the simplest thing is to change your your url not to include the pk in the url, but for the view to get that as part of GET or POST data.

url(r'ajax/entity_name/$', EntityAjaxView.as_view(), name='entity_name'),

and the view (sorry I'm not familiar with class based views):

def entity_name(request):
    pk = request.GET.get('pk')
    ...

That seems to me to be the most elegant solution. If however you absolutely need to construct the url on the client side, you can generate a template url on the server side and then replace whatever parts you need on the client side to get the full url. This however requires more maintenance and therefore is more error prone. Simple js example of this approach:

var id_number = $('#id_endowmententity_set-' + rownum + '-id_number').val(),
    url = '{% url entity_name 0 %}'.replace('0', id_number);
$.ajax({
    type: "GET",
    url: url,
    ...
});

这篇关于在AJAX调用使用Django的URL标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆