Background Image
Table of Contents Table of Contents
Previous Page  151 / 478 Next Page
Information
Show Menu
Previous Page 151 / 478 Next Page
Page Background

1. How to tell when a URL request is for a static file, as opposed to for some HTML

that’s going to be served via a view function

2. Where to find the static file the user wants

In other words, static files are a mapping from URLs to files on disk.

For item 1, Django lets us define a URL “prefix” to say that any URLs which start with

that prefix should be treated as requests for static files. By default, the prefix is

/stat

ic/

. It’s defined in

settings.py

:

superlists/settings.py.

[

...

]

# Static files (CSS, JavaScript, Images)

#

https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL

=

'/static/'

The rest of the settings we will add to this section are all to do with item 2: finding the

actual static files on disk.

While we’re using the Django development server (

manage.py runserver

), we can rely

on Django to magically find static files for us—it’ll just look in any subfolder of one of

our apps called

static

.

You now see why we put all the Bootstrap static files into

lists/static

. So why are they not

working at the moment? It’s because we’re not using the

/static/

URL prefix. Have

another look at the link to the CSS in

base.html

:

lists/templates/base.html.

<link

href=

"css/bootstrap.min.css"

rel=

"stylesheet"

media=

"screen"

>

To get this to work, we need to change it to:

lists/templates/base.html.

<link

href=

"/static/bootstrap/css/bootstrap.min.css"

rel=

"stylesheet"

media=

"screen"

>

When

runserver

sees the request, it knows that it’s for a static file because it begins

with

/static/

. It then tries to find a file called

bootstrap/css/bootstrap.min.css

,

looking in each of our app folders for subfolders called

static

, and it should find it at

lists/static/bootstrap/css/bootstrap.min.css

.

So if you take a look manually, you should see it works, as in

Figure 7-2

.

Static Files in Django

|

123