Django -从媒体文件夹中的文件夹中加载图像

时间:2021-01-15 19:21:47

I'm having an issue where I can't load images from anywhere but my media folder.

我有一个问题,我不能从任何地方加载图片,除了我的媒体文件夹。

For example:

例如:

<img src="media/image.png" />

will load just fine

将负载很好

however if i move the "image.png" file 1 more folder deeper to:

但是如果我移动“图像”。png"文件1文件夹更深的:

<img src="media/folder/image.png" />

"image.png" will not load.

”的形象。png”不会加载。

Does anyone have any ideas as to why this is happening?

有人知道为什么会这样吗?

1 个解决方案

#1


1  

You need to use django staticfiles module. 'Static' figures out where your files are placed and redirects all requests to this folder

您需要使用django staticfiles模块。“静态”计算出文件的位置,并将所有请求重定向到此文件夹

Assuming that your image.png is located in you/yourapp/static/media/folder the following should do.

假设你的形象。png位于你/你的应用/静态/媒体/文件夹中。

{% load  static %}
<img src="{% static 'media/folder/image.png' %}" />

Read the docs about serving static files with django.

阅读关于使用django服务静态文件的文档。

#1


1  

You need to use django staticfiles module. 'Static' figures out where your files are placed and redirects all requests to this folder

您需要使用django staticfiles模块。“静态”计算出文件的位置,并将所有请求重定向到此文件夹

Assuming that your image.png is located in you/yourapp/static/media/folder the following should do.

假设你的形象。png位于你/你的应用/静态/媒体/文件夹中。

{% load  static %}
<img src="{% static 'media/folder/image.png' %}" />

Read the docs about serving static files with django.

阅读关于使用django服务静态文件的文档。