简体   繁体   中英

Load Three JS model with gltf loader in Django

I am trying to load a Three JS model in a Django(3.0.3) app on a local environment.

Template:

<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;

The page loads without any Django error and it shows the ThreeJS window which is working but the model is not loaded and in the Chrome error console, I get this error:

GET http://127.0.0.1:8000/matilda/models/matilda/scene.gltf 404 (Not Found)

I think it should not check this URL because I do not resolve it in urls.py and there is nothing in this address. It should load the model which is collected in the 'static/models/matilada' with collectstatic command.

I check these links but they do not help: * Django {% static 'path' %} in javascript file * Correctly accessing django static files from external javascript * Django: External JS using framework doesn't load * https://pypi.org/project/django-js-urls/

You need to host the website on an IP address, that lets the threejs gltf model to load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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