Trying understand static files.

52 views
Skip to first unread message

john

unread,
Apr 22, 2015, 7:35:48 PM4/22/15
to django...@googlegroups.com
Hi,
I have created a website that works well under "runserver". But when I
use nginx and uwsgi the basic website comes up but it is missing the
static file information. I have run "manage.py collectstatic" but still
no static files are used.

Reading the Django doc's tells me that I need the webserver to serve the
static files. Ok I think I can do that (maybe). But I don't understand
completely. In my templates I have links like:
<link href = "{% static 'css/bootstrap.min.css' %}" rel="stylesheet">

How does the nginx server understand to provide the css file for my html
page from the code above?

I read that I can add a 'location' in the nginx config file but don't
understand how nginx would understand to provide it when my template is
called.

Thanks for the help in advance.

Johnf


Aaron C. de Bruyn

unread,
Apr 22, 2015, 7:47:14 PM4/22/15
to django...@googlegroups.com
Have you looked through the deployment documentation yet?
(https://docs.djangoproject.com/en/1.8/howto/deployment/)

-A
> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5538303D.2080202%40jfcomputer.com.
> For more options, visit https://groups.google.com/d/optout.

Lachlan Musicman

unread,
Apr 22, 2015, 7:48:06 PM4/22/15
to django...@googlegroups.com
Oh my aching head and static files.

STATIC_URL points to the namespace that the files will be served from:
eg mydomain.com/static
To set this up, in nginx/apache you need a stanza that looks like:

location /static {
alias /var/www/trees/opentrees/static;
}

STATIC_ROOT is where those files are served from - in this case,
/var/www/trees/opentrees/static
This is also where collectstatic *puts* the files it finds when it's
run. This directory, apparently (I discovered last night), should
never
be in your version control (git, svn, hg)

STATICFILES_DIRS *is* in your version control. Each of these
directories should be listed. This is where collectstatic *finds*
static files to copy to STATIC_ROOT,
Your proj/app structure looks like this:

proj/static
proj/app/static

Add each of these to STATICFILES_DIRS.

Admin, and other pip installed apps, will automatically be included (I think).

I hope this helps.

cheers
L.
------

I'm treading carefully
but it's the time of night
the snowy light
the subway roar
and the whispered fights
exciting sights
but it's not enough
I thought it was
I wish it was
I thought it was
-----
You name it - The Cannanes with Explosion Robinson.

john

unread,
Apr 22, 2015, 7:56:31 PM4/22/15
to django...@googlegroups.com
Thanks that helps with the nginx configure file. But what I don't
understand is how the template tells nginx what to serve? There has to
be some sort of black magic here! As I understand the process - the
client asks for a page - django routes via urls - it finds the template
and processes it - uses the static file location - django grabs the
static file and sends it. How does Nginx know what to do?

Being a newbie - I'd like to understand why I'm adding a 'locaiton' and
how the nginx becomes aware that it is needed.

Mike Dewhirst

unread,
Apr 22, 2015, 7:58:28 PM4/22/15
to django...@googlegroups.com
You probably want three locations ... here are mine:

location /static/ {
root /home/mike/envs/pq5/project;
access_log off;
log_not_found off;
}

location /robots.txt {
root /home/mike/envs/pq5/project/static;
access_log off;
log_not_found off;
}

location /favicon.ico {
root /home/mike/envs/pq5/project/static/img;
access_log off;
log_not_found off;

So whenever a client browser requests something prefixed by /static/
nginx sees that and substitutes the value of "root". Therefore,
collectstatic has to put your css files exactly where your template says
to look for them.

Collectstatic knows exactly where by checking the value in
settings.STATIC_ROOT

hth

mike

john

unread,
Apr 22, 2015, 8:10:50 PM4/22/15
to django...@googlegroups.com
Thanks guys - that worked!!!!!!!!!

I'm going to call it black magic - because to be truthful I don't
understand how it really works. If anyone has a better link than
Django's on static files that explains what is really happening - it
would be very helpful.

Johnf

C. Kirby

unread,
Apr 23, 2015, 10:35:15 AM4/23/15
to django...@googlegroups.com
Hi John,
It can be a little confusing. Once you are in production and not using runserver, django does not serve the static files. This is what happens:

The template expands this snippet with a tag:
<link href = "{% static 'css/bootstrap.min.css' %}" rel="stylesheet">

into (something like) this (Taking into account the STATIC_URL that Lachlan discussed above):
<link href = "/mystaticfolder/css/bootstrap.min.css" rel="stylesheet">
i.e. a regular link.

When the browser sees that it requests the page <host>/mystaticfolder/css/bootstrap.min.css from the webserver, which in your case is nginx.
The location directive in your nginx configuration sets up where on the filesystem that url resolves to, and it serves the content back to the client.

The reason this is great it because, since no python is needed to serve the js, you don't have the overhead of python and django to serve static content (and webservers are highly tund to serve static content _fast_). For high load sites this can result in noticeable resource savings.

Hope that helps,
Kirby 

john

unread,
Apr 23, 2015, 10:46:50 AM4/23/15
to django...@googlegroups.com
Thank you very very much!  It's no longer black magic.   I did not think about the fact that the template just becomes a simple HTML page and the client browser has to get the javascript as requested.

Johnf
--
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 http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages