簡體   English   中英

為什么在遵循 Django 關於 static 文件的文檔后沒有顯示我的圖像?

[英]Why aren't my images showing up after following Django's documentation on static files?

我在正在構建的網站中顯示圖像時遇到了一些問題。 我按照 Django static 文件文檔中的說明進行操作。 也就是說,我有:

  1. 驗證我的settings.py文件包含STATIC_URL = 'static/'
  2. 將以下代碼的等效項添加到我的模板中:
    {% load static %}
    <img src="{% static 'my_app/example.jpg' %}" alt="My image">
  1. 將我的圖像存儲在 `my_app/static/my_app/

當我導航到相關頁面時,我沒有收到任何錯誤。 只是一個空的 div 圖像應該在哪里。

似乎在過去,這個問題已經通過使用render()示例)解決了。 但是,我使用的是 Django 4.x,以及render() function。

為什么我的圖片不顯示?

還有一個要求,那就是將 static url 設置添加到我們的urlpatterns

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

這來自同一文檔中的“在開發期間提供 static 文件”部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM