当创建图像的函数需要参数(如坐标列表)时,如何在模板上显示动态图像?

时间:2012-12-06 13:06:24

标签: django

简而言之问题:

当创建图像的函数需要参数时,如何在模板上显示动态图像,如任意长的坐标列表。?

例如<img src="{% url getComplexImg coords %}"/>,其中coords是由整数或整数元组组成的任意长度的列表,getComplexImg是一个视图,它返回一个图像作为HttpResponse,其中mimetype是图像/ png,以及使用坐标列表生成图像的位置。

编辑:我在底部添加了一个解决方案(灵感来自第一个答案)。


问题,长版本

以下是我正在尝试做的简化

urls.py:

urlpatterns = patterns('',
    url(r'^getSimpleImg$', views.getSimpleImg, name='getSimpleImg'),
    url(r'^getComplexImg$', views.getComplexImg, name='getComplexImg'),
    url(r'^showAllImgs$', views.showAllImgs, name='showAllImgs')
    )

views.py:

...
from django.template import Context, loader
...

def getSimpleImg(request):
    return HttpResponse(getImg.simple(), mimetype="image/png")

def getComplexImg(request, coords_1, coords_2):
    return HttpResponse(getImg.complex(coords_1,coords_2), mimetype="image/png")

def showAllImgs(request):
    coords_1, coords_2 = data_module.get_all_coordinates()
    context = Context({
        'coords_1':coords_1,
        'coords_2':coords_2})
    template = loader.get_template('showall.html')
    return HttpResponse(template.render(context))

'data_module.get_all_coordinates()'只是一个返回两个任意长度列表的方法(列表可以包含整数或整数元组)。

showall.html:

<html>
...
<img src="{% url getSimpleImg %}"/>
<img src="{% url getComplexImg coords_1 coords_2 %}"/>
...
</html>

很容易让Django显示从'getSimpleImg'返回的图像,因为它不需要任何列表参数。但我正在努力让Django显示复杂的图像,因为我不知道如何从模板中传递任意列表作为参数。

上面的代码显然不起作用,因为Django无法在urls.py中查找“{%url getComplexImg coords_1 coords_2%}”。

如何解决这类问题?

我应该以某种方式以上下文方式发送图像吗?像:

views.py:

...
def showAllImgs(request):
coords_1, coords_2 = data_module.get_all_coordinates()
context = Context({
    'img_1':getImg.simple(),
    'img_2':getImg.complex(coords_1,coords_2)})
template = loader.get_template('showall.html')
return HttpResponse(template.render(context))

showall.html:

<html>
...
<img src="{{ img_1 }}"/>
<img src="{{ img_2 }}"/>
...
</html>

(上面不起作用,只是为了说明我的意思)

或者我应该以某种方式将所需的所有东西导入到模板中,然后从那里创建和显示图像......?像:

showall.html:

{% import data_module %}
{% import getImg %}
<html>
...
<img src="{% getImg.simple() %}"/>
<img src="{% getImg.complex(data_module.get_all_coordinates()) %}"/>
...
</html>


解决方案,灵感来自第一个答案

我现在通过传递上下文来解决它:

Context({'coords_1':"_".join([str(v) for v in coords_1])})

并使用以下内容捕获urls.py中的网址:

url(r'^getComplexImg/(?P<coords_1>[0-9_\-]+)/$', views.getComplexImg, name='getComplexImg')

在我的getComplexImg视图中,我将弦乐列表转换回真实列表:

coords_1 = [int(v) for v in coords_1.split('_')]

它有效,我现在很满意,但我觉得这可能不是最佳解决方案

1 个答案:

答案 0 :(得分:1)

您的urls.py似乎格式不正确。

您可以通过您的网址传递变量。通过使用正确的语法(变量作为正则表达式),您可以在调用url(){% url %}时引用这些变量,就像您尝试的那样。

这个简单的例子来自这里: http://www.djangobook.com/en/2.0/chapter08.html#using-default-view-arguments

urls.py

urlpatterns = patterns('',
    (r'^blog/$', views.page),
    (r'^blog/page(?P<num>\d+)/$', views.page),
)

views.py

def page(request, num='1'):
    # Output the appropriate page of blog entries, according to num.
    # ...

这里重要的一点是: (?P<num>\d+)

然后,您可以将<here>中的任何内容传递给您的查看功能,例如:def my_view(request, here): ...

在您的情况下,您将替换以下内容:

url(r'^getSimpleImg$', views.getSimpleImg, name='getSimpleImg'),
...

对于这样的事情:

url(r'^getSimpleImg/(?P<coord1>\d+)/(?P<coord2>\d+)/$', views.getSimpleImg, name='getSimpleImg'),
...

你们很多人不想在你的网址中使用命名变量,但如果你不能使用{% url %},那么就需要找到另一种解决方案。