目录“/static/” STATICFILES_DIRS 设置中不存在。 (视觉工作室)

发布于 2025-01-10 14:28:15 字数 2447 浏览 1 评论 0原文

当我想要“python manage.py makemigrations”时,它返回以下内容。

System check identified some issues:

WARNINGS
?: (staticfiles.W004) The directory '/static/' in the STATICFILES_DIRS setting does not exist.

我应该如何解决这个问题?

from pathlib import Path import os  BASE_DIR = Path(__file__).resolve().parent.parent  ALLOWED_HOSTS = ['*']  INSTALLED_APPS = [     'django.contrib.admin',     'django.contrib.auth',     'django.contrib.contenttypes',     'django.contrib.sessions',     'django.contrib.messages',     'django.contrib.staticfiles',      'products',     'orders', ]

MIDDLEWARE = [     'django.middleware.security.SecurityMiddleware',     'django.contrib.sessions.middleware.SessionMiddleware',     'django.middleware.common.CommonMiddleware',     'django.middleware.csrf.CsrfViewMiddleware',     'django.contrib.auth.middleware.AuthenticationMiddleware',     'django.contrib.messages.middleware.MessageMiddleware',     'django.middleware.clickjacking.XFrameOptionsMiddleware', ]

ROOT_URLCONF = ‘rtu_server.urls'  TEMPLATES = [     {         'BACKEND': 'django.template.backends.django.DjangoTemplates',         'DIRS': [],         'APP_DIRS': True,         'OPTIONS': {             'context_processors': [                 'django.template.context_processors.debug',                 'django.template.context_processors.request',                 'django.contrib.auth.context_processors.auth',                 'django.contrib.messages.context_processors.messages',             ],         },     }, ]  WSGI_APPLICATION = ‘rtu_server.wsgi.application'

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.sqlite3',         'NAME': BASE_DIR / 'db.sqlite3',     } }  AUTH_PASSWORD_VALIDATORS = [     {         'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',     },     {         'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',     },     {         'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',     },     {         'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',     }, ] 

LANGUAGE_CODE = 'en-us'  TIME_ZONE = 'Asia/Hong_Kong'  USE_I18N = True  USE_TZ = True   STATIC_URL = 'static/'  STATICFILES_DIRS = [     os.path.join(BASE_DIR, 'static') ]  DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'  MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

When I want to "python manage.py makemigrations", it returns the following.

System check identified some issues:

WARNINGS
?: (staticfiles.W004) The directory '/static/' in the STATICFILES_DIRS setting does not exist.

How should I solve this issue?

from pathlib import Path import os  BASE_DIR = Path(__file__).resolve().parent.parent  ALLOWED_HOSTS = ['*']  INSTALLED_APPS = [     'django.contrib.admin',     'django.contrib.auth',     'django.contrib.contenttypes',     'django.contrib.sessions',     'django.contrib.messages',     'django.contrib.staticfiles',      'products',     'orders', ]

MIDDLEWARE = [     'django.middleware.security.SecurityMiddleware',     'django.contrib.sessions.middleware.SessionMiddleware',     'django.middleware.common.CommonMiddleware',     'django.middleware.csrf.CsrfViewMiddleware',     'django.contrib.auth.middleware.AuthenticationMiddleware',     'django.contrib.messages.middleware.MessageMiddleware',     'django.middleware.clickjacking.XFrameOptionsMiddleware', ]

ROOT_URLCONF = ‘rtu_server.urls'  TEMPLATES = [     {         'BACKEND': 'django.template.backends.django.DjangoTemplates',         'DIRS': [],         'APP_DIRS': True,         'OPTIONS': {             'context_processors': [                 'django.template.context_processors.debug',                 'django.template.context_processors.request',                 'django.contrib.auth.context_processors.auth',                 'django.contrib.messages.context_processors.messages',             ],         },     }, ]  WSGI_APPLICATION = ‘rtu_server.wsgi.application'

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.sqlite3',         'NAME': BASE_DIR / 'db.sqlite3',     } }  AUTH_PASSWORD_VALIDATORS = [     {         'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',     },     {         'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',     },     {         'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',     },     {         'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',     }, ] 

LANGUAGE_CODE = 'en-us'  TIME_ZONE = 'Asia/Hong_Kong'  USE_I18N = True  USE_TZ = True   STATIC_URL = 'static/'  STATICFILES_DIRS = [     os.path.join(BASE_DIR, 'static') ]  DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'  MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪梦之恋 2025-01-17 14:28:15

它只是说您在 settings.py 中创建了 STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")],但没有任何静态项目中的文件夹。

现在这不是一个大问题,因为您可能没有使用外部 CSS 或 JS。但如果你想解决这个问题,你可以在你的项目中创建一个static文件夹。

It just says that you created STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] in your settings.py, but you didn't have any static folder in your project.

It is not a big issue right now because you might not be using external CSS or JS. But if you want to solve the issue, you can just create a static folder in your project.

十二 2025-01-17 14:28:15

如果您仍然看到错误,则在根目录中创建一个文件夹“static”。

我相信问题就在于此,

BASE_DIR = Path(__file__).resolve().parent.parent

假设这是您的 "settings.py" 所在的文件夹树:

E:\\Project>Project>mySettings>settings.py

然后你必须像下面这样修改 BASE_DIR,

BASE_DIR = Path(__file__).resolve().parent.parent.parent

每个文件夹一个 .parent

Create a folder "static" in root directory if you still see error, then.

The problem is with this i believe,

BASE_DIR = Path(__file__).resolve().parent.parent

Let's say this is tree of folder where your "settings.py" is:

E:\\Project>Project>mySettings>settings.py

Then you have to modify BASE_DIR little bit like below,

BASE_DIR = Path(__file__).resolve().parent.parent.parent

one .parent for each folder

无悔心 2025-01-17 14:28:15

因此,为了让您更轻松,您需要在此处导航到创建项目的位置并创建一个名为 static 的文件夹。不要尝试在 IDE 编辑器中执行此操作,因为您最终可能会感到困惑。我假设您正在尝试将 CSS 等文件链接到您的 Django 项目。这样做,你的问题就会得到解决。

另外,请记住在 HTML 页面的标题处加载静态语句。

举个例子:

<link rel='stylesheet' href='{% static 'style.css' %}' type='text/css'>
{% load static %}

So what you need to do here to make it easier for you is to navigate to the location where you have your project created and create a FOLDER called static. Don't try to do it for your IDE editor as you might end up getting confused. I am assuming you are trying to link up files such as CSS to your Django project. Do this and your problem will be solved.

Also, remember to load your static statement at the header of your HTML page.

As an example:

<link rel='stylesheet' href='{% static 'style.css' %}' type='text/css'>
{% load static %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文