Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Django3中,如何使用Javascript访问模板目录?_Javascript_Python_Jquery_Django_Ajax - Fatal编程技术网

在Django3中,如何使用Javascript访问模板目录?

在Django3中,如何使用Javascript访问模板目录?,javascript,python,jquery,django,ajax,Javascript,Python,Jquery,Django,Ajax,也许我走错了方向。我想在用户单击某个时,使用jQuery.load()替换-元素的内容。但是我无法访问要替换内容的html文件。无论我将加载指向哪个url,控制台总是说:“GEThttp://127.0.0.1:8000/templates/nav_form.html 404(未找到)“ 我的Javascript(jQuery)是: 因此,在本例中,我使用url模板/nav_form.html。当我将其替换为。/templates/nav_form.html或/templates/nav_for

也许我走错了方向。我想在用户单击某个
时,使用
jQuery.load()
替换
-元素的内容。但是我无法访问要替换内容的html文件。无论我将加载指向哪个url,控制台总是说:
“GEThttp://127.0.0.1:8000/templates/nav_form.html 404(未找到)“

我的Javascript(jQuery)是:


因此,在本例中,我使用url
模板/nav_form.html
。当我将其替换为
。/templates/nav_form.html
/templates/nav_form.html
时,控制台中的消息保持不变,告诉我他找不到
http://127.0.0.1:8000/templates/nav_form.html

通过将模板目录添加到URL.py,可以将其公开为静态文件

from django.conf.urls.static import static

PATH = '/get-static-templates/'
ROOT = '/var/www/html/templates/'

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(PATH, document_root=ROOT)

这里的路径是url路径,即
localhost:8000/get static template/
,ROOT是您要提供服务的文件夹的绝对路径。

我认为您需要定义一个返回该模板的url,然后使用该url获取模板并以您想要的方式加载它。如果html文件是静态的,并且没有被url使用,您可以将其保存在静态文件夹中,添加静态url并使用静态加载。如果正确,则需要加载包含模板的完整新html文档。我想要的是使用ajax只替换-元素的内容。因此,在不从服务器加载新文档的情况下,在URL.py中创建一个单独的路径,并将其与模板文件夹一起发送到服务器
from django.conf.urls.static import static

PATH = '/get-static-templates/'
ROOT = '/var/www/html/templates/'

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(PATH, document_root=ROOT)