【问题标题】:Content not being served from STATIC_ROOT未从 STATIC_ROOT 提供内容
【发布时间】:2020-10-20 10:41:05
【问题描述】:

这是我在 django 的第一个项目,所以我不知道很多事情。首先,我想从 django STATIC_ROOT 提供静态文件,而不是 STATICFILES_DIRS。 这是我的项目树:

    src >>
        ...db.sqlite3
        ...manage.py
        ...mainapp (main module) 
              ...__init__.py
              ...settings.py
              ...urls.py
              ...wsgi.py

        ...myapp (django app)
              ...Migrations(Folder)
              ...__init__.py
              ...admin.py
              ...apps.py
              ...models.py
              ...tests.py
              ...views.py
              
         ...accounts (django app)
              ...Migrations(Folder)
              ...__init__.py
              ...admin.py
              ...apps.py
              ...models.py
              ...tests.py
              ...views.py

         ...templates
              ...myapp
                   ...home.html

              ...accounts
                   ...
                   ...
              
         ...static 
              ...myapp 
                  ...css
                      ...bootstrap.css
                      ...home.css
                  ...js
                  ...images
                  
              ...accounts 
                  ...
                  ...
                  ...
                  
          ...static_cdn (STATIC_ROOT)
              ...admin
                  ...
                  ...
                  ...

              ...myapp 
                  ...css
                  ...js
                  ...images
                  
              ...accounts 
                  ...css
                  ...js
                  ...images

在 settings.py 中:

INSTALLED_APPS = [
    'myapp',
    'accounts',
    .....,
    .....,
    .....,
    'django.contrib.staticfiles',
]

.......
.......
.......

STATIC_URL = '/static/'

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

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

在 urls.py 中:

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
.......

from myapp.views import (
        .......,
      .......,
)

from accounts.views import (
      .......,
      .......,
)

urlpatterns = [
      .......,
      .......,
      .......,
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

在模板中的 home.html 中:

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
      <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
    <title>.......</title>
    <link rel="stylesheet" type="text/css" href="{% static 'myapp/css/bootstrap.css'%}"/>
      <link rel="stylesheet" type="text/css" href="{% static 'myapp/css/home.css'%}"/>
</head>

<body>
  .......
  .......
  .......
</body>

</html>

然后我运行命令:

(clashsite) D:\DjangoSite\clashsite\src>python manage.py collectstatic

然后会发生以下情况:

211 static files copied to 'D:\DjangoSite\clashsite\src\static_cdn'.

现在,端点,我想要的是:我不想从树中的“static”文件夹中提供静态文件,而是从“static_cdn”文件夹中提供静态文件。更准确地说,我不想提供静态文件来自 STATICFILES_DIRS ,但来自 STATIC_ROOT。 但内容仅来自静态文件夹。如果我删除静态文件夹,静态文件不提供来自 static_cdn 或STATIC_ROOT。我该怎么做??

【问题讨论】:

    标签: django


    【解决方案1】:

    @ruddra 已经向你解释过了。

    要在生产环境中为您的静态资产static_cdn 提供服务,您需要像apachenginx 这样的Web 服务器,但您可以使用xamppwampserver 在本地为您的Django 应用程序提供服务(模拟生产如您所说的环境)并像任何网络应用程序一样访问它 以真实域名提供服务(例如:http://hellodjango.local/

    apache (https://pypi.org/project/mod-wsgi/) 全局安装mod_wsgi(意思是停用首先您的Django应用程序的当前虚拟环境)

    您需要对wsgi.py 文件进行一些更改以包含activate_this.py 脚本。

    然后为您的应用创建vhost conf 文件

    <VirtualHost *:80>
    
        ServerName hellodjango.local
    
        # the root project folder
        DocumentRoot "C:/myapps/hellodjango"
        
        WSGIScriptAlias / "C:/myapps/hellodjango/src/mainapp/wsgi.py"
        
        <Directory "C:/myapps/hellodjango/src/mainapp">
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        Alias /static_cdn "C:/myapps/hellodjango/src/static_cdn"
        <Directory "C:/myapps/hellodjango/src/static_cdn">
            Require all granted
        </Directory>
    
        # Alias /media_cdn "C:/myapps/hellodjango/src/media_cdn"
        # <Directory "C:/myapps/hellodjango/src/media_cdn">
        #     Require all granted
        # </Directory>
    
    ...
    </VirtualHost>
    

    别忘了在windowshosts文件中添加127.0.0.1 hellodjango.local并重启apache服务器。

    注意:您也可以像STATIC_URLSTATIC_ROOT 一样配置MEDIA_URLMEDIA_ROOT(用于文件上传)。

    希望这能给你带来缺失的拼图。

    【讨论】:

      【解决方案2】:

      STATIC_ROOT 的用法有点不同,它与生产部署有关。您在这里看到的是 Django 在DEBUG=True 模式下提供的内容。它正在通过staticfiles app 提供内容。

      STATIC_ROOT 是运行 collect static 时存储静态文件的目录。但是从那个目录提供服务并不是 django 会做的。如果您处于生产模式(又名DEBUG=False),Django 将不会提供任何静态内容。然后你需要使用像 NGINX 或 Apache 这样的反向代理服务器来提供来自STATIC_ROOT 目录的静态文件。

      更多信息请访问documentation

      【讨论】:

      • 但是有没有办法做到这一点?我的意思是,从静态根服务到模拟生产环境
      • 我已经解释了如何做到这一点(通过使用单独的服务器,如 NGINX 或 Apache),这就是 STATIC_DIR 的设计目的。
      猜你喜欢
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      相关资源
      最近更新 更多