简体   繁体   English

在 Django 中,static 文件应该在哪个目录?

[英]In Django, which directory should static files be?

I have my static files in sub_app directory.我在 sub_app 目录中有我的 static 文件。 I will name the folder sub_app where view.py , model.py are located.我将命名view.pymodel.py所在的文件夹sub_app And when I run python manage.py findstatic it returns sub_app\static folder.当我运行python manage.py findstatic它返回sub_app\static文件夹。 I have another folder name as main_app, where settings.py file is located.我有另一个文件夹名称为 main_app, settings.py文件所在的位置。 These two folders and manage.py file is located in a root folder.这两个文件夹和manage.py文件位于根文件夹中。 I have no issue when DEBUG=True , but when I run DEBUG=False , I got following warning DEBUG=True时我没有问题,但是当我运行DEBUG=False时,我收到以下警告

venv\lib\site-packages\whitenoise\base.py:115: UserWarning: No directory at: root\static\
  warnings.warn(u"No directory at: {}".format(root))

Here are static files settings这里是 static 文件设置

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Where should my static files?我的 static 文件应该放在哪里?

You can put your static files anywhere, as long as you let your application know where they are.您可以将 static 文件放在任何地方,只要您让应用程序知道它们在哪里。 You have it configured that你已经配置好了

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

but you say that you have your static files in sub_app\static .但你说你在sub_app\static中有你的 static 文件。 The error is telling you that it's checking the root folder.该错误告诉您它正在检查根文件夹。 You need to change your configuration to您需要将配置更改为

STATIC_URL = '/sub_app/static/'
STATIC_ROOT = '/sub_app/static'

Create a directory named static inside the app directory where the "view.py, model.py are located."在“view.py、model.py 所在的应用程序目录”中创建一个名为static的目录。 Inside static you need to create another directory with the same name as of the app.static 中,您需要创建另一个与应用程序同名的目录。 Inside that folder you can create your static files which include anything like your css files在该文件夹中,您可以创建 static 文件,其中包括 css 文件之类的任何内容

Just add this line in the settings.py of your app只需在应用程序的 settings.py 中添加这一行

STATIC_URL = '/static/' STATIC_URL = '/静态/'

As you mentioned sub_app you should create sub_app/static/sub_app directory.正如您提到的 sub_app 您应该创建 sub_app/static/sub_app 目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM