Re: difficulty with static files

58 views
Skip to first unread message

Tom Evans

unread,
Nov 29, 2012, 9:56:52 PM11/29/12
to django...@googlegroups.com
On Thu, Nov 29, 2012 at 6:00 PM, Sammy <hayssa...@gmail.com> wrote:
> Hello django experts
> I am unable to get my static files to work. Here are my settings:
>
> projectfiles
> |
> |-----myproject
> | |
> | |-----static
> | | |
> | | |-----css
> | | |-----js
> | |-----__init__.py
> | |-----settings.py
> | |-----urls.py
> | |-----wsgi.py
> |
> |-----myapp
> |
> |-----templates
>
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
>
> settings.py
> import os
> SITE_ROOT = (os.path.realpath(os.path.dirname(__file__))).replace('\\','/')
> DEBUG = True
> MEDIA_ROOT = (os.path.join(SITE_ROOT, '/static')).replace('\\','/')
> MEDIA_URL = '/static/'
> STATIC_ROOT = ''
> STATIC_URL = ''

I think you are confused about MEDIA and STATIC. MEDIA is where files
are uploaded to by users, STATIC is where files are served from, the
_ROOT postfix denotes a directory path, the _URL postfix a URL path.

So you should have STATIC_ROOT=os.path.join(...) and STATIC_URL='/static/'

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

But do you have the staticfiles app in INSTALLED_APPS?

> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
>
> urls.py
> urlpatterns = patterns('',
> (r'^myurl/$', myview),
> )
>
>
> from myproject.settings import DEBUG

You should always import settings like this:

from django.conf import settings
if settings.DEBUG

https://docs.djangoproject.com/en/1.4/topics/settings/#using-settings-in-python-code

(It's really important actually, it can cause hard to find bugs!)

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

This is not needed if you are using runserver in development, if you
are not using runserver, then it is better to use the shortcut methods
provided

https://docs.djangoproject.com/en/1.4/howto/static-files/#serving-static-files-in-development

Cheers

Tom

Sammy

unread,
Jan 12, 2013, 6:59:26 PM1/12/13
to django...@googlegroups.com
Tom - thanks so much.  I was able to resolve it.


On Thursday, November 29, 2012 10:00:26 AM UTC-8, Sammy wrote:
Hello django experts
I am unable to get my static files to work.  Here are my settings:

projectfiles
|
|-----myproject
|     |
|     |-----static
|     |     |
|     |     |-----css
|     |     |-----js
|     |-----__init__.py
|     |-----settings.py
|     |-----urls.py
|     |-----wsgi.py
|
|-----myapp
|
|-----templates


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


settings.py
import os
SITE_ROOT = (os.path.realpath(os.path.dirname(__file__))).replace('\\','/')
DEBUG = True
MEDIA_ROOT = (os.path.join(SITE_ROOT, '/static')).replace('\\','/')
MEDIA_URL = '/static/'
STATIC_ROOT = ''
STATIC_URL = ''
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


urls.py
urlpatterns = patterns('',
    (r'^myurl/$', myview),
)


from myproject.settings import DEBUG
if DEBUG:
    urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', 

{'document_root': 'static'}))
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

the app works fine but no connection to my css's or javascripts.

I'm using pycharm if that makes a difference.

Any help would be greatly appreciated.



 

Tom Evans

unread,
Jan 16, 2013, 2:20:28 PM1/16/13
to django...@googlegroups.com
On Wed, Jan 16, 2013 at 10:38 AM, Fred Kingham <fredk...@gmail.com> wrote:
> not sure if this is the right place to post this, but doesn't this -
>
> https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development
>
> "If using runserver for local development (and the DEBUG setting is True),
> you no longer need to add anything to your URLconf for serving static files
> in development."
>
> contradict this -
>
> https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development
>
> "
>
> To enable this view if you are using some other server for local
> development, you'll add a couple of lines to your URLconf. The first line
> goes at the top of the file, and the last line at the bottom:
>
> from django.contrib.staticfiles.urls import staticfiles_urlpatterns
>
> # ... the rest of your URLconf goes here ...
>
> urlpatterns += staticfiles_urlpatterns()
> "
>
>

No. You've snipped the context in the second extract which makes it
clear that it is not required using runserver:

"""
This view is automatically enabled and will serve your static files at
STATIC_URL when you use the built-in runserver management command.

To enable this view if you are using some other server for local
development, you'll add a couple of lines to your URLconf. The first
line goes at the top of the file, and the last line at the bottom:
"""

Cheers

Tom
Message has been deleted

Fred Kingham

unread,
Jan 16, 2013, 4:54:25 PM1/16/13
to django...@googlegroups.com
ok, I do think its confusing, but I'm happy to admit it might just be me.
Reply all
Reply to author
Forward
0 new messages