什么是Django相当于Rails中的“replace_html”?

时间:2021-11-18 12:32:55

I'm wondering what a controller - such as the following Rails controller - would look like in Django, to perform an ajax update of the page after an asynchronous form submission (assuming the following is correct):

我想知道一个控制器 - 比如下面的Rails控制器 - 在Django中看起来像是在异步表单提交后执行页面的ajax更新(假设以下是正确的):

def create
@omelet = Omelet.new(params[:omelet])
render :update do |page|
  if @omelet.save
    page.replace_html 'notice', 'Omelet was successfully cooked'
  else
    page.replace_html 'notice', 'Sorry - the omelet could not be cooked'
  end
  page.replace_html 'omelets', :partial => 'meals/omelet_list',
    :locals => {:omelets =>  @omelet.meals.omelets }
end

end

I understand that Django does not have the same built-in ajax libraries. Does this mean something like the above would be much harder/more verbose in Django?

据我所知,Django没有相同的内置ajax库。这是否意味着上面的内容在Django中会更难/更冗长?

The "replace_html" calls really make things easy in Rails - I'm hoping there is an easy equivalent in Django.

“replace_html”调用确实让Rails变得简单 - 我希望在Django中有一个简单的等价物。

Also, does Django have the notion of 'partials' - as Rails does?

另外,Django是否具有'部分'的概念 - 就像Rails一样?

4 个解决方案

#1


The "page.replace_html" in Rails is really a manifestation of RJS - that is Javascript that is generated by the server (and the correct MIME type is sent, etc) and then the client takes that chunk of Javascript and inserts it somewhere in the DOM (probably replacing some other element as specified by DOM ID). So it really takes 2 coordinations: the server-side to send the Javascript back to the client and then the client to receive and act upon the received data.

Rails中的“page.replace_html”实际上是RJS的一种表现形式 - 即由服务器生成的Javascript(以及发送的正确MIME类型等),然后客户端获取Javascript的大块并将其插入到DOM(可能替换DOM ID指定的其他元素)。所以它真的需要2个协调:服务器端将Javascript发送回客户端,然后客户端接收并根据接收的数据进行操作。

I dont know if Django supports this but you would be better off googling around for "rjs django" and the like, in that essentially you are looking for the RJS equivalent in Django.

我不知道Django是否支持这个,但你最好用谷歌搜索“rjs django”之类的东西,因为基本上你正在寻找Django中的RJS等价物。

#2


Rails likes to abstract client-side implementation details and tries to make this appear to be a server-side concern. Django assumes you already have your own client-side AJAX techniques and simply facilitates any server-side support that you need.

Rails喜欢抽象客户端实现细节,并试图使其看起来像是服务器端关注的问题。 Django假设您已经拥有自己的客户端AJAX技术,并且只是简化了您需要的任何服务器端支持。

Different philosophies (Django never tries to write your HTML or Javascript for you except in the most trivial of cases).

不同的哲学(Django从不试图为您编写HTML或Javascript,除非在最琐碎的情况下)。

The typical solution would be a bit of JQuery or whatever to make a normal HTTP request and then slot the HTML or JSON reponse into the DOM as required (this is usually a one liner in JQuery).

典型的解决方案是使用JQuery或其他任何方式来生成普通的HTTP请求,然后根据需要将HTML或JSON响应插入到DOM中(这通常是JQuery中的一个内容)。

Just use normal Django views and templates to create your response.

只需使用普通的Django视图和模板即可创建响应。

#3


Currently Django is a server-side framework only. So you will need to use some Javascript framework to help you with this task.

目前Django只是一个服务器端框架。因此,您需要使用一些Javascript框架来帮助您完成此任务。

Frameworks such as JQuery have an .load() method where you can just insert HTML into DOM, you could grab whatever response, a string message or complex HTML from a Django view and embed it in your page.

像JQuery这样的框架有一个.load()方法,您只需将HTML插入DOM,就可以从Django视图中获取任何响应,字符串消息或复杂的HTML,并将其嵌入到您的页面中。

------ HTML ------
<div id="message"></div>
<input class="button" value="Get message"/>

------ Javascript -------
$("input").click(function(){
  $("#message").load("/get_message/");
});

------ Django view ------
def get_message(request):
  return HttpResponse("Your message")

Short answer, there isn't an equivalent, you'd have to write your own.

简短的回答,没有相应的,你必须自己写。

#4


I don't of anything in Django where you control specific parts of the page via the view functions (server side code).

我不会在Django中通过视图函数(服务器端代码)控制页面的特定部分。

The closest thing you can do in the view function is return plain text instead of html (or, return data in JSON format), then you have to use Javascript in the template file (which displays html) that sends the request and upon receiving the response, decides what parts of the DOM to manipulate (or what other things to do)

你在视图函数中最接近的事情是返回纯文本而不是html(或以JSON格式返回数据),然后你必须在发送请求的模板文件(显示html)中使用Javascript,并在收到响应,决定要操作的DOM的哪些部分(或其他要做的事情)

UPDATE

If you're used to program in this way, you might consider writing a little javascript framework that handles typical tasks like this,

如果您习惯以这种方式编程,您可以考虑编写一个小的javascript框架来处理这样的典型任务,

(actually I'm starting to like the idea, might implement it myself!!)

(实际上我开始喜欢这个想法,可能会自己实现!!)

#1


The "page.replace_html" in Rails is really a manifestation of RJS - that is Javascript that is generated by the server (and the correct MIME type is sent, etc) and then the client takes that chunk of Javascript and inserts it somewhere in the DOM (probably replacing some other element as specified by DOM ID). So it really takes 2 coordinations: the server-side to send the Javascript back to the client and then the client to receive and act upon the received data.

Rails中的“page.replace_html”实际上是RJS的一种表现形式 - 即由服务器生成的Javascript(以及发送的正确MIME类型等),然后客户端获取Javascript的大块并将其插入到DOM(可能替换DOM ID指定的其他元素)。所以它真的需要2个协调:服务器端将Javascript发送回客户端,然后客户端接收并根据接收的数据进行操作。

I dont know if Django supports this but you would be better off googling around for "rjs django" and the like, in that essentially you are looking for the RJS equivalent in Django.

我不知道Django是否支持这个,但你最好用谷歌搜索“rjs django”之类的东西,因为基本上你正在寻找Django中的RJS等价物。

#2


Rails likes to abstract client-side implementation details and tries to make this appear to be a server-side concern. Django assumes you already have your own client-side AJAX techniques and simply facilitates any server-side support that you need.

Rails喜欢抽象客户端实现细节,并试图使其看起来像是服务器端关注的问题。 Django假设您已经拥有自己的客户端AJAX技术,并且只是简化了您需要的任何服务器端支持。

Different philosophies (Django never tries to write your HTML or Javascript for you except in the most trivial of cases).

不同的哲学(Django从不试图为您编写HTML或Javascript,除非在最琐碎的情况下)。

The typical solution would be a bit of JQuery or whatever to make a normal HTTP request and then slot the HTML or JSON reponse into the DOM as required (this is usually a one liner in JQuery).

典型的解决方案是使用JQuery或其他任何方式来生成普通的HTTP请求,然后根据需要将HTML或JSON响应插入到DOM中(这通常是JQuery中的一个内容)。

Just use normal Django views and templates to create your response.

只需使用普通的Django视图和模板即可创建响应。

#3


Currently Django is a server-side framework only. So you will need to use some Javascript framework to help you with this task.

目前Django只是一个服务器端框架。因此,您需要使用一些Javascript框架来帮助您完成此任务。

Frameworks such as JQuery have an .load() method where you can just insert HTML into DOM, you could grab whatever response, a string message or complex HTML from a Django view and embed it in your page.

像JQuery这样的框架有一个.load()方法,您只需将HTML插入DOM,就可以从Django视图中获取任何响应,字符串消息或复杂的HTML,并将其嵌入到您的页面中。

------ HTML ------
<div id="message"></div>
<input class="button" value="Get message"/>

------ Javascript -------
$("input").click(function(){
  $("#message").load("/get_message/");
});

------ Django view ------
def get_message(request):
  return HttpResponse("Your message")

Short answer, there isn't an equivalent, you'd have to write your own.

简短的回答,没有相应的,你必须自己写。

#4


I don't of anything in Django where you control specific parts of the page via the view functions (server side code).

我不会在Django中通过视图函数(服务器端代码)控制页面的特定部分。

The closest thing you can do in the view function is return plain text instead of html (or, return data in JSON format), then you have to use Javascript in the template file (which displays html) that sends the request and upon receiving the response, decides what parts of the DOM to manipulate (or what other things to do)

你在视图函数中最接近的事情是返回纯文本而不是html(或以JSON格式返回数据),然后你必须在发送请求的模板文件(显示html)中使用Javascript,并在收到响应,决定要操作的DOM的哪些部分(或其他要做的事情)

UPDATE

If you're used to program in this way, you might consider writing a little javascript framework that handles typical tasks like this,

如果您习惯以这种方式编程,您可以考虑编写一个小的javascript框架来处理这样的典型任务,

(actually I'm starting to like the idea, might implement it myself!!)

(实际上我开始喜欢这个想法,可能会自己实现!!)