Groovietunes
Groovietunes

Reputation: 663

Django loading static files?

Django is not loading my static files. However it is loading my templates which are in the static folder. Also chrome is not seeing the static files either I'm not even getting a 404 error and yes they are linked in the html...but they are not showing up in the network panel

Heres my settings.py file

    STATIC_ROOT = ''

    STATIC_URL = '/static/'

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

Here's my html

    <head>
        <title>MySite | Home</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" type='text/css' src='css/normalize.css'>
        <link href='http://fonts.googleapis.com/css?family=Questrial|Josefin+Sans' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" type="text/css" src='css/main.css'>
        <script src="https://maps.googleapis.com/maps/api/js"></script>
    </head>

Sorry I know this question has been asked multiple times and i've tried all those solutions with no luck. I've spent 2 days trying to figure this out

Upvotes: 1

Views: 5701

Answers (2)

Moeed Rafique
Moeed Rafique

Reputation: 1

In your setting.py file add this

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

create a folder in your app named static and in your template file add this {% load static %}

Upvotes: 0

Brandon Taylor
Brandon Taylor

Reputation: 34553

The approach I take with static files is basically what's outlined in the docs.

In local development, Django will serve static files automatically from the directory specified in STATIC_ROOT as long as django.contrib.staticfiles is in your INSTALLED_APPS and DEBUG = True

My project structure typically looks like this:

my_project/
    some_app/
    lib/
    static/  <------- STATIC_ROOT - used in production. `collectstatic` collects here
    static_assets/ <- STATICFILES_DIRS - used in local dev
        css/
        less/
        js/
        images/
    templates/  <---- TEMPLATE_DIRS
    manage.py

settings.py is typically:

INSTALLED_APPS = (
    . . .
    'django.contrib.staticfiles',
    . . .
)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static_assets'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

Then in templates, you can again use the staticfiles app's template tags to build out paths to static files:

{% load static from staticfiles %}

<link rel="stylesheet" href="{% static 'css/normalize.css' %}" />

Also note that with <link> tags, you need to use the href property for the url instead of src.

Upvotes: 3

Related Questions