简体   繁体   English

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

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

Currently I have 6-7 apps present inside my Django project.目前,我的 Django 项目中有 6-7 个应用程序。 In settings.py for STATICFILES_DIRS I need to specify assets directory full path present inside all my apps which is cumbersome in case when I increase my apps everytime I need to add the path here.在 STATICFILES_DIRS 的 settings.py 中,我需要指定所有应用程序中存在的资产目录完整路径,这在我每次需要在此处添加路径时增加应用程序时会很麻烦。 Isn't there one setting with which I can specify just the assets folders and the collectstatic command will look for static files inside assets in all my apps?难道没有一种设置可以让我只指定资产文件夹,并且 collectstatic 命令将在我所有应用程序的资产中查找静态文件吗?

This is what I currently have:这是我目前拥有的:

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",
]

What I needed was something like this and the collectstatic would have gone to all my apps(1-6) looking for the assets directory.我需要的是这样的东西,而 collectstatic 会去我所有的应用程序(1-6)寻找资产目录。

STATICFILES_DIRS = [
    'templates/assets',
]

Is there any way to do the same?有没有办法做同样的事情?

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

Django convention states that we can have all our static files under every app in the static directory. Django 约定规定我们可以在静态目录中的每个应用程序下拥有所有静态文件。 Its better to create another folder inside the static directory with the same name as project(projectname/appname/static/appname/[all static files and folders])最好在静态目录中创建另一个与项目同名的文件夹(项目名称/应用程序名称/静态/应用程序名称/[所有静态文件和文件夹])

The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.默认是查看 STATICFILES_DIRS 中定义的所有位置以及 INSTALLED_APPS 设置指定的应用程序的“静态”目录。

Note : % static always looks under the STATIC_ROOT specified in settings.py, STATICFILES_DIRS and under static directory inside each app.注意: % static总是在 settings.py、STATICFILES_DIRS 中指定的 STATIC_ROOT 和每个应用程序内的静态目录下查找。 index.html索引.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设置.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

In Production :在生产中:

Common tactic is to serve static files from a cloud storage provider like Amazon's S3 and/or a CDN (content delivery network).常见的策略是从像 Amazon 的 S3 和/或 CDN(内容交付网络)这样的云存储提供商提供静态文件。 This lets you ignore the problems of serving static files and can often make for faster-loading web pages (especially when using a CDN).这使您可以忽略提供静态文件的问题,并且通常可以加快网页的加载速度(尤其是在使用 CDN 时)。

The basic outline of putting static files into production consists of two steps: run the collectstatic command when static files change, then arrange for the collected static files directory (STATIC_ROOT) to be moved to the static file server and served.将静态文件投入生产的基本大纲包括两个步骤:当静态文件发生变化时运行 collectstatic 命令,然后安排将收集的静态文件目录(STATIC_ROOT)移动到静态文件服务器并提供服务。

But here we are serving the static files from the same machine using the apache web server.但是在这里,我们使用 apache Web 服务器从同一台机器上提供静态文件。

Everytime static files are changed the command needs to be executed每次更改静态文件时都需要执行命令

python manage.py collectstatic

And specify the static url in the urls.py并在 urls.py 中指定静态 url

urls.py网址.py

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

Since all the static files will be served by the web server itself, we need to specify the location in our config file.由于所有静态文件都将由 Web 服务器本身提供,因此我们需要在配置文件中指定位置。 Please note that its important to run collectstatic command in production when static files are served by apache web server.请注意,当静态文件由 apache Web 服务器提供时,在生产中运行 collectstatic 命令很重要。 It looks for static files in the location specified by you in config file which is usually the STATIC_ROOT directory.它在配置文件中指定的位置查找静态文件,该位置通常是 STATIC_ROOT 目录。

/etc/apache2/sites-available/000-default.conf /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>

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

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