如何从Ajax()获取Django模板的值? [英] How to get values from the Ajax() into Django template?

查看:160
本文介绍了如何从Ajax()获取Django模板的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django模板,它显示从数据库中收到的多个值,并将其传递给模板,如下所示:



在views.py

  def test1(requests):
requests.get ...
requests.get ...
requests.get ...
someQuery =select id from table;
executeQ(someQuery)
someQuery =从表中选择id
executeQ(someQuery)
someQuery =从表中选择id
executeQ(someQuery)
context = Data1,Data2,Data3
返回render_to_response('test1 / index.html',上下文)

in template / test1 / index.html

 < html> ; 
......

< table>
< th> header1< / th>
< th> header2< / th>
< th>标题3< / th>

{%for context in context%}
< td> row.1< / td>
< td> row.2< / td>
< td> row.3< / td>
{%endfor%}

现在,我想要的是更新这些row.1 ,row.2,row.3通过Ajax,而不是每次都重新编码页面。数据来自数据库。那么在哪里和如何可以放入一些Ajax(),所以这发生在Django?

解决方案

你将需要添加一些客户端侧面代码(JavaScript)除了服务器端代码(Python)。



一个常见的方法是使用jQuery ajax() 方法向服务器发送ajax请求,使用您的django应用程序,将响应发回客户端,然后操纵DOM。



所以您的客户端代码需要




  • 发送 XMLHttpRequest 至服务器(通常使用 $。ajax()

  • 如果
    请求成功,修改DOM(通常位于 成功回调
    函数)

  • <迭代JSON响应中的数据以创建表行所需的标记,然后使用JavaScript / jQuery将上一行替换为新的标记(如 here


您的django应用程序需要



我还建议您阅读关于使用django和ajax的这个很好的答案(包括jQuery $。ajax() 方法)。



另外值得一提的是,您不需要使用jQuery来执行ajax步骤 - 您可以使用纯JavaScript生成Ajax请求 - 但是它是一种流行的方法,并且用户友好。


I have a Django template where it shows multiple values received from the database and passes it to the template, something like following:

in views.py

def test1(requests):
  requests.get...
  requests.get...
  requests.get...
  someQuery = "select id from table;"
  executeQ(someQuery)
  someQuery = "select id from table;"
  executeQ(someQuery)
  someQuery = "select id from table;"
  executeQ(someQuery)
  context = Data1, Data2, Data3
  return render_to_response('test1/index.html', context)

in template/test1/index.html

<html>
  ...... 

<table>
<th> header1 </th>
<th> header2 </th>
<th> header3 </th>

{% for row in context %}
 <td> row.1 </td>   
 <td> row.2 </td>
 <td> row.3 </td>
{% endfor %}

Now, what I want is to update those row.1, row.2, row.3 via Ajax without realoding the page everytime. The data come from the database. So where and how can I put in some Ajax() so this happens with Django?

解决方案

You will need to add some client side code (JavaScript) in addition to you server side code (Python).

A common approach is to use the jQuery ajax() method to send an ajax request to the server, handle this using your django application, send a response back to the client and then manipulate the DOM.

So your client side code needs to

  • send an XMLHttpRequest to the server (commonly using $.ajax())
  • modify the DOM if the request was successful (commonly inside the $.ajax() success callback function)
  • you might do this by iterating over the data in the JSON response to create the markup needed by the table row, and then replace the previous row with this new markup using JavaScript/jQuery (as shown here)

And your django application needs to

  • match the URL pattern inside a URL dispatcher
  • query your database inside the associated view
  • serialise the returned data into JSON (you can use the python JSON module)
  • return this JSON via a HttpResponse (this SO question talks about JSON and HttpResponses)

I also recommend you read this excellent answer about using django and ajax (including examples of the jQuery $.ajax() method).

It's also worth mentioning that you do not have to use jQuery for the ajax step - you can generate Ajax requests with pure JavaScript - however it is a popular approach and pretty user friendly.

这篇关于如何从Ajax()获取Django模板的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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