jQuery .load()在Django模板中不起作用

Cha*_*May 3 html python django jquery django-templates

我想做的是在模板中呈现一个html“ a”文件。并使用jquery .load()将其他html文件嵌入此a.html中。但是我的以下代码失败了。

以下是我的a.html内容:

<html>
 <head>
  {% load staticfiles %}
   <script type="text/javascript" src="{% static "js/jquery-1.10.2.js" %}" />
   <script>
   $(function(){
     $("#includeContent").load("abc.html");
   });
   </script>
 </head>
 <body> 
   <div id="includeContent"></div>  
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)

以下是我的abc.html:

<html>
<body>
<!-- Hello -->
 <p>Hello world </p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

以下是我的应用程序树:

- App:
  views.py
  models.py
  - static
    -js:
     jquery-1.10.2.js
  - template:
    a.html
    abc.html
Run Code Online (Sandbox Code Playgroud)

在Firebug调试器中,我可以看到jquery-1.10.2.js的源代码,因此我认为jquery脚本已成功包含在内。

我要嵌入到a.html中的abc.html应该显示“ Hello world”,但是加载a.html之后我什么都看不到。

有没有办法解决这个问题?

添加“模板”的设置:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
Run Code Online (Sandbox Code Playgroud)

]

Wto*_*wer 5

Django在服务器端工作。它根据请求准备响应。另一方面,jQuery在客户端工作。所有jQuery的事情都发生在用户的浏览器上,并且load只会向Django发出另一个请求。但是jQuery无法以任何方式访问Django文件,因为用户的浏览器无法访问经过正确配置的服务器中的任何项目文件(希望出于安全考虑)。

因此,在您的代码中jQuery将请求特定的url abc.html,因此您需要分别在您的URL中定义一个,urls.py或者基本上是重新考虑您的设计。