Django使用jquery将文本文件加载到html文件中



我想使用Django从w3schools.com实现这个示例代码。代码使用jqueryload()函数将文本文件加载到html文件中。代码如下所示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt");
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>
</body>
</html>

我的问题是现在,我如何存储我的文本文件,使加载函数可以访问它在我的Django项目?

将'demo_test.txt'放在静态文件夹中,并确保在设置中指定静态url。假设你的静态url是"指向文件夹"静态"。在这种情况下,你可以使用$("#div1").load("/cdn/demo_test.txt");

Insettings.py:

STATIC_URL = '/cdn/'
STATIC_ROOT = '/path/to/your/static'

最新更新