skip to Main Content

I having difficulty serving static files with Django 1.10, uWSGI and Nginx.

I have an index.html file, which contains CDN’s.

The Django documentation, which is here says to “transfer the static files to the storage provider or CDN.” What does that mean, “transfer to the CDN”? Isn’t the CDN where you get files from?

settings.py contains,

STATIC_URL = '/static/'

STATIC_ROOT = 'nonAppBoundStaticDirectory'

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

Running

$ python manage.py collectstatic

does this place all CDN’s in my directory ‘nonAppBoundStaticDirectory’?

If so then how do i use that in the template?

Excerpt from index.html

  <!-- Bootstrap -->
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/css/bootstrap.min.css">

Excerpy from /etc/nginx/nginx.conf

server {
    # the port your site will be served on
    listen  80;
    # the domain name it will serve for
    server_name example.com;   # substitute your machine's IP address or FQDN
    charset     utf-8;

    #Max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
                alias /home/ofey/djangoForum/fileuploader/uploaded_files;      # your Django project's media files
    }

        location /static {
                alias /home/ofey/djangoForum/noAppBoundStaticDirectory;     # your Django project's static files
        }
........

Thanks,

2

Answers


  1. It might be just a spelling mistake. In your settings.py you have an extra n in the directory name.

    STATIC_ROOT = 'nonAppBoundStaticDirectory'
    

    In your nginx config, you have a different spelling.

    location /static {
        alias /home/ofey/djangoForum/noAppBoundStaticDirectory; 
    

    Make sure that the path point to the exact same directory. It’s can be a good idea to use absolute paths in both cases.

    Login or Signup to reply.
    1. Working in development server

    If you are working in development server- remove STATIC_ROOT from settings.py. Your settings.py should now look like this:

        STATIC_URL = '/static/'
    
        STATICFILES_DIRS = [
            os.path.join(BASE_DIR, 'static'),
        ]
    
    1. Production

    If you are working in production- remove STATICFILES_DIRS from settings.py. Your settings.py should now look like this:

        STATIC_URL = '/static/'
    
        STATIC_ROOT = 'nonAppBoundStaticDirectory'
    

    And don’t forget to run:

        python manage.py collectstatic
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search