trouble with setting static for img and css

23 views
Skip to first unread message

johnf

unread,
May 11, 2017, 8:28:53 AM5/11/17
to Django users
Hi folks,

I have researched this issue and I know all I have to do is read the
doc's. But I can seem to get the correct path working to allow the
website to find the correct files in my Media,CSS and JS folders.

I'm using 1.11.x and have the following setup in OS file system.

I have set DEBUG = True

/z/pes_django1.11/pesweb/ is where my manage.py is located - it runs
just fine.

my files are in

/z/pes_django1.11/pesweb/static/static/css/ - this of course is for CSS

/z/pes_django1.11/pesweb/static/static/js/ -this of course is for JS

/z/pes_django1.11/pesweb/static/static/img/ -this of course is for
images like favicon.ico

In my settings.py file I have tried many combinations such as:

STATIC_URL = '/pesweb/static/static/'

STATIC_URL = '/static/static/'

STATIC_URL = '/pesweb/static/'


So would some kind soul please tell me what I need in STATIC_URL, and
MEDIA_URL.

Thanks in advance,

Johnf



Matthew Pava

unread,
May 11, 2017, 9:22:27 AM5/11/17
to django...@googlegroups.com
Typically, we set it to "/static/".
STATIC_URL = "/static/"

Don't forget to run your collectstatic management command.
python manage.py collectstatic
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4bf10da8-e78b-147c-78dd-0d4206143ba9%40jfcomputer.com.
For more options, visit https://groups.google.com/d/optout.

Carl

unread,
May 11, 2017, 9:40:18 AM5/11/17
to django...@googlegroups.com
Hi Johnf,

Subject: Nginx + Gunicorn + Django Example

For a website using Nginx + Gunicorn + Django, try using STATIC_ROOT along
with the following settings.

If you login as user z, then usually your web project would be
set up in a path like /home/z/pesweb/
Note: The folder, pes_django1.11, seems out of place. Is it your
virtual environment? Usually your virtual environment would be
in a path like /home/z/pesweb/web_env/  Your specific folder setup
affects your gunicorn and nginx definitions (see below).

# If your website can be browsed at http://www.pesweb.com and
# your static files are browser-accessible at
# http://www.pesweb.com/static/ then your STATIC URL is /static/
STATIC_URL = '/static/'

# If you choose to keep the out-of-place folder, pes_django1.11, then
# STATIC_ROOT = '/home/z/pes_django1.11/pesweb/static/'  This is not
# recommended since it seems to complicate things without any advantage.
#
# Absolute path to the directory static files should be collected to.
STATIC_ROOT = '/home/z/pesweb/static/'


And, your nginx definition might be like the following one:
server {
    listen 80;
    server_name pesweb.com www.pesweb.com;

    # Give a blank page if browsing the IP.
    root /var/www/html;

    # Only static files need to be browser-accessible.
    location /static/ {
        root /home/z/pesweb
        try_files $uri $uri/ =404;
    }
}

And, your gunicorn definition might be like the following one:
[Unit]
Description=gunicorn-pesweb daemon
After=network.target

[Service]
User=z
Group=www-data
WorkingDirectory=/home/z/pesweb
ExecStart=/home/z/pesweb/web_env/bin/gunicorn --bind unix:/home/z/pesweb/pesweb.sock pesweb.wsgi:application

[Install]
WantedBy=multi-user.target

Hope this helps!

Cheers,
Carl



-------- Original Message --------
Subject: trouble with setting static for img and css

John Fabiani

unread,
May 11, 2017, 9:59:57 AM5/11/17
to django...@googlegroups.com
First thanks for responding.
Yes there is a website www.pesprograms.com but I'm using the code in the development and it is a virtualenv.  The path I provided is in fact the real path (not my 'home').  

When I run collectstatic I get none of my static files - actually I'm just looking for favicon.ico.  But I do want to understand what I'm doing wrong for all the static files.  Question: am I suppose run collectstatic each time I change the STATIC_URL?
Johnf 

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Antonis Christofides

unread,
May 11, 2017, 10:13:05 AM5/11/17
to django...@googlegroups.com

Hello,

Hopefully these drawings of mine can help.

How Django static files work in production

If you have trouble understanding STATIC_ROOT, STATIC_URL, collectstatic, and the web server’s configuration, it’s because some things are better said with pictures than with text. Here are the pictures.

https://djangodeployment.com/2016/11/21/how-django-static-files-work-in-production/

Regards,

A.

Antonis Christofides
http://djangodeployment.com

Antonis Christofides

unread,
May 11, 2017, 10:13:32 AM5/11/17
to django...@googlegroups.com

No, if you change the STATIC_URL you don't need to run collectstatic again.

Antonis Christofides
http://djangodeployment.com
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
May 11, 2017, 12:40:09 PM5/11/17
to django...@googlegroups.com

On Thursday 11 May 2017 05:28:33 johnf wrote:

 

> I have researched this issue and I know all I have to do is read the

> doc's. But I can seem to get the correct path working to allow the

> website to find the correct files in my Media,CSS and JS folders.

 

Read this wikipedia piece. And then consider that STATIC_URL is the path component and STATIC_ROOT is the local file system.

 

Unfortunately in an effort to trim down settings.py STATIC_ROOT has been removed in the generated boiler plate somewhere in 1.9 I think, so people fail to understand how things work from just reading the settings as all they see is STATIC_URL.

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages