在AJAX调用中使用Django URL标记

时间:2022-09-04 12:37:49

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:

有很多关于Django和AJAX使用的帖子和页面,我在过去一天左右阅读了数百个,寻找这个问题的答案。快速概述:

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

示例中的5月显示了这样的硬编码URL:

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

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

或者一些使用URL模板标签,但没有参数:

$.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中包含Django样式的参数。这是我的网址定义:

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:

是的,我正在使用基于类的视图,它基于DetailView。默认情况下,此视图会在URL中提供pk值,而在我将使用的普通模板中:

{% 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):

提供链接。在我的代码中,我想获取输入框中为pk值输入的值。这是我的JavaScript代码片段(不起作用):

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?

所以,我的问题是,我可以使用带有输入框值的URL模板标记吗?

(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.)

(我知道我可以使用POST而不是GET并在POST数据中传递id_number,但这对于DetailView不起作用。)

Thanks in advance for your help.

在此先感谢您的帮助。

1 个解决方案

#1


11  

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.

Django是一个服务器端应用程序。 Javascript是客户端的。 Django模板在服务器上呈现,因此在服务器端评估{%url entity_name id_number%},然后将其值返回给客户端。正因为如此,你不可能将Django模板与javascript结合起来。但是,您可以采取一些措施来解决问题。

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.

由于您正在进行ajax调用,并且ajax调用依赖于某些用户输入,因此客户端向服务器发送任何类型的用户输入的最佳途径是使用查询字符串(URL后面的东西)或者发送POST数据。所以最简单的方法是更改​​你的网址,不要在网址中包含pk,而是将视图作为GET或POST数据的一部分。

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:

在我看来,这是最优雅的解决方案。但是,如果您绝对需要在客户端构建URL,则可以在服务器端生成模板URL,然后在客户端替换所需的任何部分以获取完整的URL。然而,这需要更多维护,因此更容易出错。这种方法的简单js示例:

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

#1


11  

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.

Django是一个服务器端应用程序。 Javascript是客户端的。 Django模板在服务器上呈现,因此在服务器端评估{%url entity_name id_number%},然后将其值返回给客户端。正因为如此,你不可能将Django模板与javascript结合起来。但是,您可以采取一些措施来解决问题。

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.

由于您正在进行ajax调用,并且ajax调用依赖于某些用户输入,因此客户端向服务器发送任何类型的用户输入的最佳途径是使用查询字符串(URL后面的东西)或者发送POST数据。所以最简单的方法是更改​​你的网址,不要在网址中包含pk,而是将视图作为GET或POST数据的一部分。

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:

在我看来,这是最优雅的解决方案。但是,如果您绝对需要在客户端构建URL,则可以在服务器端生成模板URL,然后在客户端替换所需的任何部分以获取完整的URL。然而,这需要更多维护,因此更容易出错。这种方法的简单js示例:

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