首页 > 解决方案 > 在 Django 中使用 gltf 加载器加载三个 JS 模型

问题描述

我正在尝试在本地环境的 Django(3.0.3) 应用程序中加载三个 JS 模型。

模板:

<script>
    var address_to_model ="{% static 'models/matilda/scene.gltf' %}"; // Pass this variable to .js file
</script>
<script src="{% static 'js/matilda.js' %}"></script>

matilda.js:

var loader = new THREE.GLTFLoader( );
loader.load( address_to_model, function ( gltf ) {
        scene.add( gltf.scene );
    }, undefined, function ( error ) {
        console.error( error );
} );
renderer.outputEncoding = THREE.sRGBEncoding;

页面加载时没有任何 Django 错误,它显示 ThreeJS 窗口正在工作,但模型未加载,在 Chrome 错误控制台中,我收到此错误:

获取http://127.0.0.1:8000/matilda/models/matilda/scene.gltf 404(未找到)

我认为它不应该检查这个 URL,因为我没有在 urls.py 中解析它,并且这个地址中没有任何内容。它应该使用collectstatic命令加载在“static/models/matilada”中收集的模型。

我检查了这些链接,但它们没有帮助: * javascript 文件中的 Django {% static 'path' %} *从外部 javascript 正确访问 django 静态文件 * Django:使用框架的外部 JS 不加载 * https://pypi. org/project/django-js-urls/

标签: javascriptpythondjangothree.js

解决方案


您需要在 IP 地址上托管网站,以便加载 threejs gltf 模型。


推荐阅读