[英]Static files are not fetching in django
我的設置說明。
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/css')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
此外,我已將 static 文件添加到 urls.py urlpatterns += staticfiles_urlpatterns()
我的 HTML 模板
{% load static %}
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Things I eat</title>
</head>
<body>
<h1> Hello world</h1>
</body>
查找靜態命令findstatic
:
> python manage.py findstatic styles.css
Found 'styles.css' here:
/project_name/static/css/styles.css
我使用django == 3.0.7
我遵循了類似問題的建議[1 , 2 , 3]但是我的 static 文件在渲染時沒有獲取。 對調試有什么建議嗎?
編輯:我的 static 目錄結構:
static
-- css
---- styles.css
我遇到了同樣的問題並嘗試了很多解決方案,但最后對我來說下面的方法有效,我希望它也對你有用
首先不需要添加STATICFILES_DIRS
和STATIC_ROOT
設置.py
DEBUG = True
//make sure that django.contrib.staticfiles is present in your installed apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
//add this line at the end
STATIC_URL = '/static/'
並且似乎您已加載 static 但未添加任何 css 文件,為此在您的 html 頁面 Z0997939540DBZF31
<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">
您是否包含樣式表鏈接? <link rel="stylesheet" type="text/css" href="{% static 'css/styles.css ' %}">
我總是單獨給出目錄,然后再添加 CSS 文件夾。
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
然后在 html 文件中:
<link rel="stylesheet" href="{% static 'css/addrecord.css' %}">
欲了解更多信息。 https://docs.djangoproject.com/en/3.0/howto/static-files/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.