1

I have spent hours on this and looked everywhere, but wasn't able to figure out, so I beg help from more experienced once.

It is not my first time I am creating a django app, so I referenced what I did before, but nothing helps. I am trying to serve static files using "static" template tag, but it doesn't work.

I am pretty sure I set up everything correctly(following tutorials and my previous projects; i'll provide settings below), but when I run server locally, it can't find my static files. When I replace static template tag with {{STATIC_URL}} tag it works. I even type the direct path in my browser to the static files, all of them work. I get output of CSS and JS files. It is driving me crazy, that the static template tag is not working.

Here are my settings:

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = 'staticfiles'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/w/static/'

# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
#ADMIN_MEDIA_PREFIX = '/admin/media/'

# Additional locations of static files
STATICFILES_DIRS = (
   os.path.join(PROJECT_DIR, 'static/'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.

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

INSTALLED_APPS = (
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.sites',
   'django.contrib.messages',
   'django.contrib.staticfiles',)

Btw, i checked the path for static folder and it is correct. I deliberately didn't include the name of the app, so it is there. I also include {%load static from staticfiles%} tag. As i mentioned only static tag isn't working. Please, somebody explain what stupid mistake I am making.

thanks

rahanar
  • 48
  • 1
  • 6

1 Answers1

1

I'm using django 1.6.1

Here is my setup:

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

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

My file structure looks like this:

mysite
  myapp
  static
  templates
  manage.py

Edit:

It's {% load staticfiles %} not {%load static from staticfiles%}

You don't need to use STATIC_ROOT unless your trying to have a static folder in each app, I believe

I had this question For example .jpg != .jpeg! So look for that mistake as well

Community
  • 1
  • 1
user3043594
  • 166
  • 10
  • my BASE_DIR is PROJECT_DIR = os.path.join(os.path.dirname(__file__), os.pardir) and I checked it manually, it is correct. I also tried {% load staticfiles %} and i have the same problem. it is weird problem, as I have just tested my other app that runs on 1.5 and everything works. This just bugs my mind. – rahanar Jan 25 '14 at 06:21
  • BASE_DIR and PROJECT_DIR are arbitrary, they are only used to locate your STATIC and TEMPLATE and MEDIA dir... So try my set up and see if you have success. As you can see my base dir is mysite, and all my static content is in the static folder. If you don't want to try that, then I suggest you make sure you spelled everything correctly. And try basic things, i.e. eliminate to find the most likely cause. Comment so more if you need more help! – user3043594 Jan 25 '14 at 07:27
  • 1
    I don't know what sorcery it is, but it worked. My file structure is the same as yours, but using your way to find BASE_DIR made the difference. I don't know why though, because the way I did it worked before. Anyway, thank you. – rahanar Jan 25 '14 at 19:16