如何为动态生成的图像生成网址?

时间:2022-09-16 09:48:21

Only started messing around with Django yesterday and I've been stuck on this issue now for 5+ hours. I have a page which is a table, and the plan is for it to look like:

昨天才刚开始搞乱Django,我现在已经坚持了5个多小时。我有一个页面,这是一个表,计划是它看起来像:

Name  -  Total Badges -  Badges
Bob   -             1 -  @(level 3)
Mike  -             3 -  @(level 5) | &(level 7)  | $(level 19)

In reality there will likely be hundreds of unique images representing several dozen "badges" (not actually using badges... Just image representations of items with a counter overlayed on it). In the table template, I have tried half a million different ways of trying to generate a url that links to an internal image. One example that I've tried which should illustrate what my intentions are is:

实际上,可能会有数百个独特的图像代表几十个“徽章”(实际上并没有使用徽章......只是物品的图像表示,其上覆盖有计数器)。在表格模板中,我尝试了50万种尝试生成链接到内部图像的URL的不同方法。我试过的一个例子应该说明我的意图是:

...
<tr> Name </tr>
<tr> Total Badges </tr>
<tr> Badges </tr>

{% if profile %}
{% for name, badges in profile %}
  <tr>
    <td> {{ name }} </td>
    <td> {{ badges.count_all }} </td>
    {% for badge in badges %}
    {% static "MyApp/Images/{{badge}}.png" as image %}
    <img src="{{ image }}">
    {% endfor %}
    </tr>
{% endfor %}

What happens with that is it generates the circle and triangle error image. But if I hard coded {% static "MyApp/Images/AmpersandBadge100.png" %} it works. I think that instead of replacing the {{ badge }} var it is simply rendering it as "MyApp/Images/{{ badge }}.png".

这会发生什么,它会生成圆形和三角形错误图像。但是,如果我硬编码{%static“MyApp / Images / AmpersandBadge100.png”%}它可以工作。我认为不是替换{{badge}} var,而只是将其渲染为“MyApp / Images / {{badge}}。png”。

I don't even know what to google for and 4 hours of scouring over the documentation has led me to believe that static file are the only way to display internal images. But that probably isn't true.

我甚至不知道谷歌的用途和4小时的搜索文件让我相信静态文件是显示内部图像的唯一方法。但那可能不是真的。

1 个解决方案

#1


0  

I googled some more after posting and after some tinkering finally found a solution that worked.

在发布后我再搜索了一些东西,经过一些修补,最终找到了一个有效的解决方案。

{% for name, badges in profile %}

...

  <td>
  {% for badge in badges %}
     ## {{ badge }}s look like "MyApp/images/%s.png"%badge
    <img src="{% static "" %}{{ badge}}">

Not sure how it works but it does.

不知道它是如何工作的,但确实如此。

#1


0  

I googled some more after posting and after some tinkering finally found a solution that worked.

在发布后我再搜索了一些东西,经过一些修补,最终找到了一个有效的解决方案。

{% for name, badges in profile %}

...

  <td>
  {% for badge in badges %}
     ## {{ badge }}s look like "MyApp/images/%s.png"%badge
    <img src="{% static "" %}{{ badge}}">

Not sure how it works but it does.

不知道它是如何工作的,但确实如此。