简体   繁体   English

我如何在 django 中提供 static 文件?

[英]How can i serve static files in django?

I have a folder static :我有一个文件夹static

static
-->css
----> main.css
-->images
----> image.png

Settings.py : Settings.py

STATICFILES_DIRS=[
    os.path.join(BASE_DIR, "static"),
]

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, "static_cdn")

I ran collectstatic我跑了collectstatic
Now I have in static_cdn : images , css and admin (never saw this last one before).现在我在static_cdn : imagescssadmin (以前从未见过最后一个)。
When I run my server, it still doesn't use static files.当我运行我的服务器时,它仍然不使用 static 文件。
How can I serve my static files to my server without using apache or nginx or that kind of stuff?如何在不使用 apache 或 nginx 或类似的东西的情况下将我的 static 文件提供给我的服务器?

Django doesn't serve static files in production but it keeps link to them. Django 在生产中不提供 static 文件,但它保持与它们的链接。 Here is where a django app called whitenoise comes to rescue when your debug is false on production.当您的调试在生产中为假时,这是一个名为 whitenoise 的 django 应用程序来救援的地方。 install whitenoise on your django app: pip install whitenoise then pip freeze > requirements.txt after this add this middleware to your django project settings在您的 django 应用程序上安装 whitenoise: pip install whitenoise然后pip freeze > requirements.txt在此之后将此中间件添加到您的 Z62AD1C2A46AFB89A36DACC 项目设置

MIDDLEWARE = [
  # 'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

then add this line然后添加这一行

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

then push this code to your production server and make sure you run collectstatic command ie python manage.py collectstatic your static files now should be working.然后将此代码推送到您的生产服务器并确保您运行collectstatic命令,即python manage.py collectstatic您的 static 文件现在应该可以工作了。

Here is the common DigitalOcean solution:这是常见的 DigitalOcean 解决方案:

Create a new space and connect to it eg via MobaXterm .创建一个新空间并连接到它,例如通过MobaXterm

Then you have access to the file storage where you basically just run collectstatic within the manage.py directory.然后您可以访问文件存储,您基本上只是在 manage.py 目录中运行collectstatic

Here is a official DigitalOcean Tutorial on deploying Django apps这是有关部署 Django 应用程序的官方 DigitalOcean 教程

You will need nginx when you deploy with DigitalOcean.使用 DigitalOcean 进行部署时,您将需要 nginx。

DigitalOcean spaces work with Amazon S3 Bucket behind the scenes, so you might directly set up a bucket and use this, but for me there is no benefit to go the hard way. DigitalOcean 空间在幕后使用 Amazon S3 存储桶,因此您可以直接设置一个存储桶并使用它,但对我来说,go 的艰难方式没有任何好处。 Just use the DigitalOcean Space since this is perfectly linked to your droplet .只需使用 DigitalOcean Space ,因为它与您的droplet完美链接。

For storing static files in production, you're going to need something like S3 bucket or some other kind of external storage.要在生产环境中存储 static 文件,您将需要 S3 存储桶或其他类型的外部存储。 I like to use whitenoise when I deploy to Heroku, since it's very simple to use.当我部署到 Heroku 时,我喜欢使用白噪声,因为它使用起来非常简单。 Here's an example of the configuration:下面是一个配置示例:

MIDDLEWARE = [
    # ...
    'whitenoise.middleware.WhiteNoiseMiddleware',

]

# ...

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'build/static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

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

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