Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serving static files django development

Tags:

django

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.

like image 237
InfinteScroll Avatar asked Feb 13 '14 21:02

InfinteScroll


2 Answers

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

like image 190
sha256 Avatar answered Nov 15 '22 05:11

sha256


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',
)
like image 26
tjati Avatar answered Nov 15 '22 04:11

tjati