【发布时间】:2019-03-24 14:50:06
【问题描述】:
我希望使用 Aframe 在我的 Django 项目中渲染 .obj 和 .mtl 文件。我想要实现的是获取一些用户输入,例如“Apple”,然后在屏幕上呈现一个苹果对象。现在,每当我对静态文件的 url 进行硬编码时,我都会得到渲染的对象。但是,当我在将输入附加到 url 后在字典中传递 url 时,对象没有被显示。查询例如:Apple。我的字典格式如下:
dict = {0 :['education/Apple.obj','education/Apple.mtl']}
我的views.py:
from django.http import HttpResponse
from django.template import loader
def viser(request):
template = loader.get_template('education/viser.html')
context = {}
context[0] = []
context[0].append("'education/Apple.obj'")
context[0].append("'education/Apple.mtl'")
query = request.POST.get('inputquery', False)
if query:
template2 = loader.get_template('education/DOG.html')
return HttpResponse(template2.render(context, request))
return HttpResponse(template.render(context, request))
硬编码的 DOG.html:
<html>
<head>
<script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>
</head>
<body>
<a-scene>
{% load static %}
<a-entity obj-model="obj: {% static 'education/Apple.obj' %}; mtl: {% static 'education/Apple.mtl' %};" ></a-entity>
<a-entity position="0 70 150">
<a-camera></a-camera>
</a-entity>
</a-scene>
</body>
</html>
动态 DOG.html
<html>
<head>
<script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>
</head>
<body>
<a-scene>
<a-sky color="#6EBAA7"></a-sky>
{% load static %}
{% for classlabel, value in context.items %}
<a-entity obj-model="obj: {% static value.0 %}; mtl: {% static value.1 %};" ></a-entity>
{% endfor %}
<a-camera position="0 0 0" look-controls wasd-controls="acceleration:1000">
<a-cursor color="yellow"></a-cursor>
</a-camera>
</a-scene>
</body>
</html>
我在调试过程中观察到的一个奇怪的事情是,每当我在硬编码的 DOG.html 文件中尝试 for 循环时,它并没有让浏览器呈现对象,但是当我去掉 for 和 endfor 标签时,我得到了渲染的对象。 for 循环是否会导致程序出现问题?
Peculiar DOG.html:不渲染对象
<a-scene>
{% load static %}
{% for index, value in context.items %}
<a-entity obj-model="obj: {% static 'education/Apple.obj' %}; mtl: {% static 'education/Apple.mtl' %};" ></a-entity>
{% endfor %}
</a-scene>
去除 for 和 endfor 后的特殊 DOG.html:渲染对象
<a-scene>
{% load static %}
<a-entity obj-model="obj: {% static 'education/Apple.obj' %}; mtl: {% static 'education/Apple.mtl' %};" ></a-entity>
</a-scene>
【问题讨论】:
-
value[0]和value[1]在 Django 模板语言中无效。只需使用value.0和value.1。 -
我尝试这样做,但浏览器上没有呈现 3D 对象(Apple)。但是,当我在静态“education/Apple.obj”中硬编码路径时,它会毫无问题地呈现出来。我做错了什么?
-
在这种情况下,edit 您的问题并显示您更新的代码。我还会尝试在您的浏览器中查看呈现的 html 的源代码,看看硬编码值和使用静态标签之间有什么区别。
-
@Alasdair 我已经更新了你问先生的问题。任何指示、建议或帮助将不胜感激。