Django collectstatic 在 django 项目中的每个应用程序中查找资产目录

Django collectstatic to look for assets directory in every app in django project

目前我的 Django 项目中有 6-7 个应用程序。在 settings.py for STATICFILES_DIRS 中,我需要指定我所有应用程序中存在的资产目录完整路径,这很麻烦,以防每次我需要在此处添加路径时增加我的应用程序。 难道没有一种设置可以让我仅指定资产文件夹,而 collectstatic 命令将在我所有应用程序的资产中查找静态文件吗?

这是我目前拥有的:

STATICFILES_DIRS = [
    BASE_DIR / "app1/templates/assets",
    BASE_DIR / "app2/templates/assets",
    BASE_DIR / "app3/templates/assets",
    BASE_DIR / "app4/templates/assets",
    BASE_DIR / "app5/templates/assets",
    BASE_DIR / "app6/templates/assets",
]

我需要的是这样的东西,collectstatic 会去我所有的应用程序 (1-6) 寻找资产目录。

STATICFILES_DIRS = [
    'templates/assets',
]

有什么方法可以做到吗?

来自 Django 4.0 文档:https://docs.djangoproject.com/en/4.0/ref/contrib/staticfiles/

Django 约定规定我们可以将所有静态文件放在静态目录中的每个应用程序下。最好在静态目录中创建另一个与项目同名的文件夹(projectname/appname/static/appname/[所有静态文件和文件夹])

默认是在 STATICFILES_DIRS 中定义的所有位置和 INSTALLED_APPS 设置指定的应用程序的 'static' 目录中查找。

注意:% static 始终在 settings.py、STATICFILES_DIRS 中指定的 STATIC_ROOT 和每个应用程序内的静态目录下查找。 index.html

<link rel="stylesheet" href="{% static 'app/css/bootstrap.min.css' %}" />
<link rel="stylesheet" href="{% static 'app/css/LineIcons.2.0.css' %}" />
<link rel="stylesheet" href="{% static 'app/css/animate.css' %}" />
<link rel="stylesheet" href="{% static 'app/css/tiny-slider.css' %}" />
<link rel="stylesheet" href="{% static 'app/css/glightbox.min.css' %}" />
<link rel="stylesheet" href="{% static 'app/css/main.css' %}" />

settings.py

STATICFILES_DIRS = [
        # specify other static directory where static files are stored n your environment.
        # by default static folders under each app are always looked into and copied.
]

STATIC_ROOT = os.path.join(BASE_DIR, 'static') # this is the folder where all static files are going to be stored after we run collectstatic command
STATIC_URL = '/static/' # this is the url from which static elements can be accessed

生产中:

常见的策略是从像亚马逊的 S3 and/orCDN(内容分发网络)这样的云存储提供商提供静态文件。这让您可以忽略提供静态文件的问题,并且通常可以制作 faster-loading 网页(尤其是在使用 CDN 时)。

生产静态文件的基本流程分为两步:运行静态文件变化时的collectstatic命令,然后安排移动收集到的静态文件目录(STATIC_ROOT)到静态文件服务器并提供服务。

但这里我们使用 apache 网络服务器从同一台机器提供静态文件。

每次更改静态文件时都需要执行命令

python manage.py collectstatic

并在urls.py

中指定静态url

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("app.urls")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

由于所有静态文件都将由网络服务器本身提供服务,因此我们需要在配置文件中指定位置。 请注意,当静态文件由 apache 网络服务器提供服务时,它对生产中的 运行 collectstatic 命令很重要。它在配置文件中指定的位置查找静态文件,通常是 STATIC_ROOT 目录。

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        #Serving static files
        Alias /static/ /etc/myproject/static/
        <Directory /etc/myproject/static>
                Require all granted
        </Directory>

        <Directory /etc/myproject/myproject>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>
        WSGIDaemonProcess myproject python-path=/etc/myproject python-home=/etc/myprojectenv
        WSGIProcessGroup myproject
        WSGIScriptAlias / /etc/myproject/myproject/wsgi.py

</VirtualHost>