Thursday, November 29, 2012

Re: difficulty with static files

On Thu, Nov 29, 2012 at 6:00 PM, Sammy <hayssam.hajar@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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment