简体   繁体   English

我有一个使用HTML,CSS和Javascript构建的静态网站。 如何将其与使用Python API访问的SQLite3数据库集成?

[英]I have a static website built using HTML, CSS and Javascript. How do I integrate this with a SQLite3 database accessed with the Python API?

Title question says it all. 标题问题说明了一切。 I was trying to figure out how I could go about integrating the database created by sqlite3 and communicate with it through Python from my website. 我试图找出如何整合由sqlite3创建的数据库并通过我的网站上的Python与它进行通信。

If any further information is required about the development environment, please let me know. 如果需要有关开发环境的任何进一步信息,请告诉我。

使用XMLHttpRequest( https://en.wikipedia.org/wiki/XMLHttpRequest )调用您的python脚本并将结果放回您的网页。

It looks like your needs has changed and you are going into direction where static web site is not sufficient any more. 看起来您的需求已经发生变化,您将进入静态网站不再充足的方向。 Firstly, I would pick appropriate Python framework for your needs. 首先,我会根据您的需要选择合适的Python框架。 if static website was sufficient until recently Django can be perfect for you. 如果静态网站足够,直到最近Django才能适合你。 Next I would suggest describing your DB schema for ORM used in chosen framework. 接下来,我建议描述在所选框架中使用的ORM的数据库模式。 I see no point in querying your DB using SQL until you would have a specific reason. 在没有特定原因的情况下,我认为使用SQL查询数据库没有意义。 And finally, I would start using static content of your website as templates, replacing places where dynamic data is required. 最后,我将开始使用您网站的静态内容作为模板,替换需要动态数据的地方。 Django internal template language can be easily used that way. Django内部模板语言可以通过这种方式轻松使用。 If not, Jinja2 also could be good. 如果没有,Jinja2也可能会很好。 My advise is base on many assumptions, as your question is quite open and undefined. 我的建议是基于许多假设,因为你的问题是非常开放和未定义的。 Anyway, I think it would be the best way to start transition period from static to dynamic. 无论如何,我认为这将是开始从静态到动态的过渡期的最佳方式。

I'm not sure if you are using JQuery at all but you should use AJAX to make calls to the python api. 我不确定你是否正在使用JQuery,但你应该使用AJAX来调用python api。

Jquery Method: http://api.jquery.com/jQuery.ajax/ Jquery方法: http//api.jquery.com/jQuery.ajax/

$.ajax({
 type: "POST", //OR GET
 url: yourapiurl,
 data: datatosend,
 success: success, //Callback when request is successful that contains the SQlite data
 dataType: dataType
});

Javascript Method: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp Javascript方法: http//www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",yourapiurl,true);
xmlhttp.send();

The responseText attribute of the XMLHttpRequest be populated with the SQlite data from the api 使用来自api的SQlite数据填充XMLHttpRequest的responseText属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM