【问题标题】:Aframe 3D Model not being rendered in Django project using static urlsAframe 3D 模型未在 Django 项目中使用静态 url 呈现
【发布时间】: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.0value.1
  • 我尝试这样做,但浏览器上没有呈现 3D 对象(Apple)。但是,当我在静态“education/Apple.obj”中硬编码路径时,它会毫无问题地呈现出来。我做错了什么?
  • 在这种情况下,edit 您的问题并显示您更新的代码。我还会尝试在您的浏览器中查看呈现的 html 的源代码,看看硬编码值和使用静态标签之间有什么区别。
  • @Alasdair 我已经更新了你问先生的问题。任何指示、建议或帮助将不胜感激。

标签: django aframe


【解决方案1】:

您对 Django 模板的一些基础知识有误。在尝试使 aframe 代码或 static 标记工作之前,请确保您可以正确访问 context 字典中的变量。

你正在渲染一个带有上下文的模板:

context = {0 :['education/Apple.obj','education/Apple.mtl']}

您不应该使用0 作为上下文变量名。将其更改为其他内容,例如obj_list

从您的代码看来,您将有很多 &lt;a-entity&gt; 标记,因此您实际上需要一个列表列表。

context = {}
context['obj_list'] = []
context['obj_list'].append(['education/Apple.obj', 'education/Apple.mtl'])

这将为您提供一个上下文字典,例如:

context = {'obj_list' :[['education/Apple.obj','education/Apple.mtl']]}

接下来,您不要在模板本身中使用context,而是使用上下文字典的键。在这种情况下,您的字典只有一个键 obj_list,因此您的循环可能类似于:

{% for item obj_list %}
    {{ item.0 }}
    {{ item.1 }}
{% endfor %}

一旦你完成了这项工作,你就可以开始使用带有静态标签和框架的变量了。

【讨论】:

  • 最后一个问题,您提到我的模板中将有多个 标签。没错,但是如果我输入“Apple and Strawberry”之类的内容,那么我的字典会看起来像这样 'dict ={'obj_list':[['apple.obj','apple.mtl'],['strawberry. obj','strawberry.mtl']]}' 那么我可以使用 item.2 给我 'strawberry.obj' 如果我不能,还有其他方法或更好的方法吗?如果我有多个键,for 循环会是什么样子?
  • 您可以使用{{ obj_list.1.0 }}{{ obj_list.1.1 }} 访问第二个列表['strawberry.obj','strawberry.mtl'] 中的值。或者,您可以使用{% for item in obj_list %} 循环遍历列表,并在循环内使用{{ item.0 }}{{ item.1 }}。在第二个循环中,item 将是列表 ['strawberry.obj','strawberry.mtl']
猜你喜欢
  • 1970-01-01
  • 2020-06-15
  • 2020-03-06
  • 2014-11-23
  • 2012-10-20
  • 2012-04-20
  • 2018-08-28
  • 2019-10-27
  • 2011-09-04
相关资源
最近更新 更多