python - 无法在我的 django 模板中加载图像

标签 python django templates web django-templates

我的项目文件夹是:

mrdoorbeen
manage.py
    mr_doorbeen
         setting.py
    mrdoorbeen
      migrations
      templates
         index.html
         profile
             profile.html

我想在我的 profile.html 文件中包含图像。 我在 profile.html 顶部使用 {% load staticfiles %} 并在图像源中使用此代码:

<img src="{% static "image/example.jpg" %}" alt="cant'load"/>

我在mr_doorbeen中创建一个文件夹,并将其命名为static,并在静态文件夹中创建一个image文件夹,并将图像放在example.jpg 但它不起作用。 我的 静态 文件夹路径错误还是什么? 我应该把我的static文件夹放在这个项目中

最佳答案

对于给定的目录结构,您可以将静态文件放在与文件 mr_doorbeen/settings.py 的目录相同的级别

mrdoorbeen
manage.py
    mr_doorbeen
      setting.py
    mrdoorbeen
      migrations
      templates
         index.html
         profile
             profile.html
    static/

您可以将静态文件设置到上述位置,如下所示。

# Static files configurations.
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

关于python - 无法在我的 django 模板中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42736773/

相关文章:

python - 如何将文档按术语数据框更改为记录单词出现的文档的数据框

python - Caffe 和 TensorFlow Protobuf - 维护不同的版本

python - Python/Django 中的 Unicode 与 UTF-8 混淆?

python - django.db.migrations.exceptions.InconsistentMigrationHistory

python - Django "deconstruct"模型字段函数的用途是什么?

c++ - 在参数推导过程中,泛型 lambda、模板函数和模板成员 operator() 之间的主要区别是什么?

python - 如何将 value_list 转换为 Django 中的列表

python - 将数字转换为二进制字符串

c++ - 模板特化 enable_if

c++ - 模板化静态成员函数指针初始化