user1035617
user1035617

Reputation: 321

how to point to static folder in django

I am learning django and I already have a bit noobish question. I can not point to my static folder and I tried all the combinations, watched people do it in youtube tutorials etc.

My settings.py looks something like this:

   STATIC_ROOT = '/home/peter/brewery/static/'
   TEMPLATE_DIRS = '/home/peter/brewery/mysite/templates/'

where brewery/ is the folder containing mysite/ and static/, mysite/ is the folder created by

   django-admin.py startproject

where settings.py also lives...

It seems that templates folder is mapped correctly, since the page renders with proper templates, it just cannot access the css in the /static/css/ folder. I show the path in my template for css like this

   <link ...  href='/static/css/brewery.css' />

I have also tried to make href absolute path on my computer and it does not work.

I am using django 1.3 and am running the server provided by django (python manage.py runserver)

Upvotes: 1

Views: 12522

Answers (2)

Aidas Bendoraitis
Aidas Bendoraitis

Reputation: 4003

In debug mode STATIC_ROOT is not used, but staticfiles_urlpatterns() provides static files from all different apps. Put your static files either into static/ directory in one of your apps or define STATICFILES_DIRS in your settings and put static files for the site there.

STATIC_ROOT is just the location where all static files are collected from all apps and STATICFILES_DIRS when you call:

python manage.py collectstatic

And it is only used in production environment.

Upvotes: 1

Dave
Dave

Reputation: 6179

1 - In your settings file, define a static url and static root like this:

STATIC_URL = '/static/'

2 - Set DEBUG = True

3 - Make sure your TEMPLATE_CONTEXT_PROCESSORS variable includes django.core.context_processors.static.

4 - Reference it in your templates like...

<link ...  href='{{ STATIC_URL }}css/brewery.css' />

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

Upvotes: 4

Related Questions