[英]Django not able to load a simple static page with static files
它是一個簡單的 static html 文件,在 ZFC35FDC70D5FC69D5EZ8628 中只有一個圖像作為 static 文件。 我已遵循 Django 文檔中提供的所有步驟,並且collectstatic
命令運行時沒有任何錯誤。
項目文件夾結構如下;
settings.py 文件;
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
DEBUG = True
home.html - 未加載圖像的文件;
{%load static%}
<!DOCTYPE html>
<html >
<head>
<title>home.html</title>
<h1>This is a test file</h1>
</head>
<body>
<img src="{% static 'images/test.png' %}">
</body>
</html>
Output:
我嘗試了以下方法;
collectstatic
命令什么都沒解決,可能是什么問題? 請幫我解決這個問題。
您有STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
而看起來您已經將文件收集到os.path.join(BASE_DIR, 'static')
,這是一個更典型的位置。 您應該更改STATIC_ROOT
以指向 static 文件夾,以便 Django 知道從哪里提供這些文件:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
您可以在static 文件部署指南中看到這一點
在你的 settings.py 中試試這個
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
def get_success_url(self, request, user):
return (get_success_url)
並從您的 urls.py 添加
urlpatterns = [
......
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns +=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
在 settings.py 中用 STATICFILES_DIRS 替換 STATICFILES_DIR
STATICFILES_DIR 是這里的主要錯誤。 將其替換為 STATICFILES_DIRS 這應該可以解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.