6

I know a million people have asked this, and I've read and read but still cannot get this to work (I think that says something about the documentation cough cough).

ONE. CSS: Here are my settings:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'))
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static")
STATIC_ROOT = '/static/'
STATIC_URL = '/static/'

also relevant:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'south',
    'blog',
)

My templates are serving up just fine, while my css is not. its located in static/originaltheme.css

I've tried both:

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

and

<link rel="stylesheet" type="text/css" href="/static/originaltheme.css">

TWO. Also, is there a way to get hardcoded urls to display? for example, lets say the content of a blog post has a hardcoded url to /static/img1.jpg, how can I do that? All of the documention points to {{ static_url }}, but im guessing that that wont get evaluated when returned from a text field of a database...

is there something to do with my urls?

from django.conf.urls import patterns, include, url

from django.contrib import admin
from django.conf.urls.static import static
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'nickswebsite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^$', 'blog.views.index'),

    url(r'^blog/view/(?P<slug>[^\.]+)',
    'blog.views.view_post',
    name='view_blog_post'),

    url(r'^blog/category/(?P<slug>[^\.]+)',
    'blog.views.view_category',
    name='view_blog_category'),

    url(r'^admin/', include(admin.site.urls)),
)

EDIT:

I tried doing this is in the urls:

urlpatterns = patterns('',
    url(r'^$', 'blog.views.index'),

    url(r'^blog/view/(?P<slug>[^\.]+)',
    'blog.views.view_post',
    name='view_blog_post'),

    url(r'^blog/category/(?P<slug>[^\.]+)',
    'blog.views.view_category',
    name='view_blog_category'),

    url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=setting.STATIC_ROOT)

urlpatterns += staticfiles_urlpatterns()

but im getting a "settings" undefined error. I tried to import settings and im still getting an error.


Edit

Okay, the documentation for serving static off development server is HORRIBLE. Whoever wrote it should really rewrite it. This guy is the only person in the entire world who seems to explain this clearly: http://agiliq.com/blog/2013/03/serving-static-files-in-django/#servee

You DO need these in the settings.py (why arent they included in the initial installaion??? what genius...):

STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
   'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

You do NOT need these: STATICFILES_DIRS = (os.path.join(BASE_DIR, "static") STATIC_ROOT = '/static/'

You do NOT need:

manage.py collectstatic

http://agiliq.com/blog/2013/03/serving-static-files-in-django/#servee:

Points to be noted

We did not make any changes to any of the static settings provided by Django to us. We left the static settings as they were in default settings.py provided by Django.

You don't need any change in your urls.py for serving your static files in development. You don't need to add staticfiles_urlpatterns(). Many a times, I got confused with this.

You do not need python manage.py collectstatic for serving static files in development.

1
  • Off-topic: I personally prefer to use {% static 'originaltheme.css' %} (needs {% load static %}) than using the STATIC_URL var. Commented Feb 13, 2014 at 22:33

4 Answers 4

2

I also fought against this particular problem. And finally came up with this blog post

Actually you don't need collectstatic , STATICFILES_FINDER and django.contrib.staticfiles. They are all related. Read the blog post

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

2 Comments

Oh man... if you're following this advice make sure you're using django from 2013. BUT BEFORE THAT, upgrade from django 2013 and disregard this all.
@DerekAdair, as you can see, this was answered in early 2014.
1

I added these lines to the bottom of my url.py:

if settings.DEBUG:
urlpatterns += patterns('',
                        url(r'^uploads/(?P<path>.*)$', 'django.views.static.serve', {
                            'document_root': settings.MEDIA_ROOT,
                        }),
)

And this in my settings.py:

STATICFILES_DIRS = ()

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

Comments

0

Did you use:

./manage.py collectstatic

Also your STATIC_ROOT should be absolute path to the directory static files should be collected to:

STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, "static"))

7 Comments

i just tried to run that and the final output was:OSError: [Errno 13] Permission denied: '/static'
now its telling me "BASEDIR" not defined... but it is! as you can see in my code. import os BASE_DIR = os.path.dirname(os.path.dirname(file)) TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] STATIC_ROOT = os.path.normpath(os.path.join(BASEDIR, "static")) STATIC_URL = '/static/'
you use BASE_DIR and in my code it is BASEDIR, fix this typo, it should be: STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, "static"))
ok. thank you. I fixed that and now ./manage.py collectstatic gives me this error: File "/Users/NickStefan/Users/NickStefan/Documents/PythonProjects/nickswebsite/.virtualenvs/nickswebsite/lib/python2.7/posixpath.py", line 61, in isabs return s.startswith('/') AttributeError: 'list' object has no attribute 'startswith'
Change [] to () like this in your settings STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
|
0

This is what I did for Django 2.0 using the above answers. So you'll want a system that makes deployment easy.

settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Serve Static in Development

STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_DIRS = [os.path.join(BASE_DIR, "appName/static/appName")]

# Serve static in production

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

Then made a directory ~/documentRoot/appName/static/appName/ and copied my png into it. Then in my template:

<head>
  <bootstrap,jquery,etc...>
  {% load staticfiles %}
</head>
<body>
<img class="mb-4" src="{% static "myImage.png" %}" alt="" width="72" height="72">
</body>

Then when you want to deploy you just run collectstatic and they'll be served in one spot. Each time you add an app, add to that list of static dirs.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.