0

Its my first project in django,so i dont know a lot of things.firstly, i want to serve static files from django STATIC_ROOT, not STATICFILES_DIRS. this is my project tree:

    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

in 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')

in 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)

in home.html in templates :

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

Then i run the command:

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

and following happens:

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

Now,the endpoint,what i want is : i dont want to serve my static files from 'static' folder in my tree, but from 'static_cdn' folder.more precise,i dont want to serve static files from STATICFILES_DIRS , but from STATIC_ROOT. But the content are served from only static folder.if i remove the static folder,static files are not served from static_cdn or STATIC_ROOT.How can i do that??

2 Answers2

1

Usage of STATIC_ROOT is bit different and it is related to production deployment. What you are seeing here is Django serving contents when you are in DEBUG=True mode. It is serving content through staticfiles app.

STATIC_ROOT is the directory where static files are stored when you run collect static. But serving from that directory is not what django will do. If you are in production mode(aka DEBUG=False), Django will not serve any static contents. Then you need to use a reverse proxy server like NGINX or Apache to serve those static files from STATIC_ROOT directory.

More information can be found regarding this on documentation.

ruddra
  • 50,746
  • 7
  • 78
  • 101
  • But is there no way to to do this??i mean,serve from static root to mimic production environment – Tawsif Riju Jun 30 '20 at 07:44
  • I have already explained how you can do that(by using a separate server like NGINX or Apache), that is what `STATIC_DIR` is designed for. – ruddra Jun 30 '20 at 07:47
1

@ruddra has already explained to you.

to serve your static assets static_cdn in production environment you need a web server like apache or nginx but you can use xampp or wampserver to serve your Django app locally (a mimic production environment as you said) and access it like any web app served with real domain name (e.g: http://hellodjango.local/)

install mod_wsgi for apache (https://pypi.org/project/mod-wsgi/) globally (meaning deactivate first the current virtual environment of your Django app)

you need to make some changes to wsgi.py file to include activate_this.py script.

and then create vhost conf file for your app

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

and don't forget to add 127.0.0.1 hellodjango.local in windows hosts file and restart apache server.

NB: you can also configure MEDIA_URL and MEDIA_ROOT (for file uploads) the same as STATIC_URL and STATIC_ROOT.

hope this brings you the missing piece of puzzle.

cizario
  • 3,995
  • 3
  • 13
  • 27