Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static files in Django 1.6

I'm probably doing multiple things wrong here because I still can't get static files to work correctly in my development environment despite closely following the tutorial. I have a feeling it's because it works slightly differently in Django 1.6, and I can only find answers for previous versions.

Here's my directory structure:

mysite
├───app1
├───mysite
│   └───templates
├───resources
├───static
│   ├───css
│   ├───fonts
│   └───js
└───app2

My installed apps, to prove I have staticfiles on:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app1',
    'app2',
)

My template and static file settings:

 # Templates
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, "mysite/templates"),
)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

I even did this in my urls.py as suggested:

urlpatterns = patterns('',
    ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

And finally, my request:

{% load staticfiles %}
<link href="{% static "css/core.css" %}" rel="stylesheet">

If I navigate directly to http://127.0.0.1/static/css/core.css, I get 'css\core.css' could not be found

Please tell me what I did wrong =[

like image 244
Chris Watts Avatar asked Mar 21 '23 17:03

Chris Watts


1 Answers

Since the static directory does not "live" in one of the apps (app1, app2 in your case), django can't find the static directory. So with your current structure you need to add the static directory to the STATICFILES_DIRS.

From the documentation:

"Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files."

Something like this:

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

See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATICFILES_DIRS

Hope this helps.

like image 113
jbub Avatar answered Apr 01 '23 00:04

jbub