如何加载上传的图像

时间:2017-08-15 10:43:42

标签: python django

在我的数据库中,Banner表的url字段值如下所示:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/horizontalRecyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

所以,在我的模板中:

uploads/images/banner.jpg

浏览器的结果如下:

{% for ban in data.banner %}

    <li><img src="{{ ban.img }}" alt="" ></li>

{% endfor %}

因此,它没有在我的项目下加载图像。

如何加载相对路径图像?

2 个答案:

答案 0 :(得分:0)

您应该使用static模板标记:

{% load staticfiles %}

<li><img src="{% static ban.img %}" alt="" ></li>

希望它有所帮助!

答案 1 :(得分:0)

您的uploads/images/个文件夹位于何处?如果它们在/ static /中,您可以在src属性中执行此操作:src="{{ '/static/' + ban.img }}"

相关问题