1

How can I view my static css files? I've set my STATIC_ROOT, and am using python manage.py runserver.

In my development environment, according the docs, I only need to place my static files (in this case, /static/css/typography.css) in my STATIC_ROOT, and python manage.py runserver will automatic create the views necessary to access it if I have DEBUG = True.

STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static")

I've also tried manually adding the views in URLConf, which won't display the css file either:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# ... the rest of your URLconf goes here ...

urlpatterns += staticfiles_urlpatterns()

In my template, the {{ STATIC_URL }} gets to the correct address (/static/css/typography.css), but it will not serve the file when I try to access it:

   <link href="{{ STATIC_URL }}css/typography.css" rel="stylesheet" type="text/css">

Notes: The other Django related static files questions on StackOverflow are over two years old. Django version 1.3b1 differentiates STATIC (static files, such as css and images) and MEDIA (user-uploaded file).

1
  • What have you set your STATIC_URL to, more precisely? Commented Jan 31, 2011 at 7:13

3 Answers 3

1

Besides. all the tries mentioned above, you must also make sure that your template is receiving the RequestContext when called from the view.

http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/

This link gives various ways of doing the same and you could choose any one. :) Also, the TEMPLATE_CONTEXT_PROCESSORS must be added to settings.py for this to take effect.

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "myapp.processor.foos",
)

Note: STATIC_ROOT is the place where all your static files are stored by Django after collecting them from STATICFILES_DIRS. Runserver will pick them up from the path mentioned in STATIC_ROOT, so STATIC_URL should point to the same location as STATIC_ROOT.

Sign up to request clarification or add additional context in comments.

Comments

0

Answered here: Django staticfiles app help

I was putting my files in STATIC_ROOT, so adding this works:

STATICFILES_DIRS = (STATIC_ROOT,)

2 Comments

STATIC_ROOT isn't served by runserver, please read the docs again: docs.djangoproject.com/en/dev/howto/static-files/…
STATIC_ROOT have to the directory on which Django collects files, not a to be a part of STATICFILES_DIRS
0

In development:

#settings.py have this by default 

STATIC_URL = '/static/'

This means when you want to use static files, django will look for them in folder 'static' in your app directory. So you need to create folder 'static' in every your app and put inside your static files.

In production:
1. Make place to hold static files from all app's

STATIC_ROOT = "/var/www/example.com/static/"

2. Run python manage.py collectstatic to collect static files from all app's in STATIC_ROOT folder.
3. In settings.py change DEBUG=False and ALLOWED_HOSTS = ['yourhostadress'].
4. Point your webserver to STATIC_ROOT folder (eg. for Apache2.4):

Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>

After this setup, your django project should be able to run (using mod_wsgi) and use static files on Apache web server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.