如何使用AJAX和Django从SQL数据库更新网站上的数据 [英] How to update data on field on a website from SQL database with AJAX and Django

查看:394
本文介绍了如何使用AJAX和Django从SQL数据库更新网站上的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小型Web应用程序,我想从服务器上的SQL数据库更新一个字段上的最新数据,而不刷新整个页面。这是可能的,如果是的话,我该怎么做?

I'm creating a little web application and I would like to update the newest data on a field from SQL database on server WITHOUT refreshing the whole page. Is this possible and if yes, how can I do that?

我想我可以不时使用jQuery刷新整个页面,但是我不认为这是非常明智的,特别是在较重的网站上,因为这将导致大量的流量。相反,我想使用数据库中最新的数据更新几个字段。

I think I could use jQuery to refresh the whole page from time to time, but I don't think that's very wise, especially on heavier sites, because that would cause a lot of traffic. Instead I would like to update just couple fields with the newest data from the database.

var refresh = function() {
   location.reload(true);
}

$(document).ready(function() {
   setTimeout(refresh, 10000);
});

这几乎已经到目前为止。

That's pretty much all I've got so far.

我发现另一个类似的问题,有人建议使用XMLHttpRequest。是否可以使用它从SQL数据库获取一些数据?

I found another similar question and there people suggested using XMLHttpRequest. Is it possible to use that to get some data from SQL database?

此外,当试图找出可能的XMLHttpRequest解决方案时,我发现可以使用一些.ASP文件将该XMLHtppRequest转换为SQL查询,但是就像我理解整个程序,需要一些Microsoft服务器框架,无论如何,与我正在编程的整个应用程序的大小和目的相比,它似乎是相当粗糙和困难的方法。特别是当我的整个网站在Linux上运行时。

Also when trying to figure out that possible XMLHttpRequest solution, I found out that it could be possible to convert that XMLHtppRequest to SQL query by using some .ASP file, but as much as I understood the whole procedure that would require some Microsoft server framework and anyway it seemed to be quite rough and hard way to go when compared to the size and purpose of the whole application I'm programming. Especially when my whole site is running on Linux.

此外,我已经阅读了很多关于websockets和观察者模式。在理论上,声音很好,正是我所需要的,但是,我对这些事情来说很新鲜,我不想这样做,如果我能避免这种情况。但是,如果有一些简单的方法来做,有人可以向我展示一个很好的教程,我也很高兴。

Moreover, I've read quite a lot about websockets and observer pattern. In theory the sound great and exactly what I need, but well, I'm quite new with these things and I wouldn't like to go that way yet, if I can avoid that. But if there's some easy way to do it and someone could show me a good tutorial about it, I'd be happy as well.

但是,在这一刻主要寻找一种方式来做这个好的老投票,无论它是多么无效。我认为它仍然比重新载入整个页面更好一些。

But at the moment I'm primarily looking for a way to do that "good old polling" no matter how ineffective it is. I think it still is a way better than to reload the whole page.

非常感谢各种建议。

推荐答案

我想你太过分了,这是一个解决的问题,几乎所有的网站今天使用它:使用AJAX。 JQuery有一个内置的ajax功能,使发送ajax请求非常容易。

I think you're over-thinking the issue. This is a solved problem and almost all websites use it today: use AJAX. JQuery has a builtin ajax feature that makes sending ajax requests very easy.

在服务器端,创建一个url端点。理想情况下,您将拥有存储在数据库中的每个资源的唯一端点。例如:

On the server side, create an url endpoint. Ideally you will have unique endpoints for each resource that you store in your database. For example:

http:// mywebsite。 com / book /?uid = 124234

此网址理论上为ID为124234的图书提供了一个CRUD界面。您可以通过发出一个POST请求。或者您可以通过发出GET请求来获取所有数据。

This url would theoretically provide a CRUD interface for the book with ID 124234. You can update this object by issuing a POST request. Or you can get all of its data by issuing a GET request.

我希望这有助于。

这篇关于如何使用AJAX和Django从SQL数据库更新网站上的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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