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

Time:05-05

Currently I have 6-7 apps present inside my Django project. 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. 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?

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.

STATICFILES_DIRS = [
    'templates/assets',
]

Is there any way to do the same?

CodePudding user response:

From Django 4.0 docs : 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. 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.

Note : % static always looks under the STATIC_ROOT specified in settings.py, STATICFILES_DIRS and under static directory inside each app. 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

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). This lets you ignore the problems of serving static files and can often make for faster-loading web pages (especially when using a 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.

But here we are serving the static files from the same machine using the apache web server.

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

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. Please note that its important to run collectstatic command in production when static files are served by apache web server. It looks for static files in the location specified by you in config file which is usually the STATIC_ROOT directory.

/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>

CodePudding user response:

You can use below code, if you don't want to do it for all apps

STATICFILES_DIRS = [
    BASEDIR / 'templates/assets',
]
  • Related