[英]Django doesn't see static files
所以问题是 django 根本无法识别静态文件。 无论我尝试过什么,它都不起作用。 我尝试将全局搜索方式更改为 css 文件的直接 url,这似乎都不起作用。
我的项目结构如下所示:
这是我的代码:
DEBUG = True
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
from django.contrib import admin
from django.urls import path
from something.views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', home),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Monkeys</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/file.css' %}"/>
</head>
可以在互联网上的任何主题中找到答案,所以我希望您能提供帮助。
当 collectstatic 运行时,默认的 STATICFILES_FINDERS 值 django.contrib.staticfiles.finders.FileSystemFinder 将从您在 STATICFILES_DIRS 中拥有的任何路径收集您的静态文件。
另一个默认的 STATICFILES_FINDERS 值 django.contrib.staticfiles.finders.AppDirectoriesFinder 将查看 INSTALLED_APPS 中任何应用程序的 /static/ 文件夹。
找到的所有静态文件都将放置在指定的 STATIC_ROOT 目录中。
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
您还可以使用 python manage.py findstatic 来查看 collectstatic 将查找哪些目录。
manage.py findstatic
目前你有一个错字 - STATIC_DIRS而不是STATICFILES_DIRS ,所以目前根本没有配置 collectstatic 。
修复它,然后再次运行collectstatic
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.