Nagri
Nagri

Reputation: 3136

Django static path is not properly set

I am trying to learn Django, In settings.py I have set:

MEDIA_ROOT = '/home/hussain/django/ratingsite/ratingsite/media'

MEDIA_URL = 'media/'

STATIC_ROOT = '/home/hussain/django/ratingsite/ratingsite/static'

STATIC_URL = '/static/'


STATICFILES_DIRS = (
    '/home/hussain/django/ratingsite/ratingsite/static',)

I have my css files in my

/home/hussain/django/ratingsite/ratingsite/static/css/default.css

but when I try to access the webpage it doesnot load the css and gives Error.

"NetworkError: 500 Internal Server Error - http://localhost:8000/static/css/default.css"

I am new to this so I dont understand what is my root directory. what runs first and how Django tries to find the resources. If someone can guide me through this it would be great.


Guyz Guyz I was just doing hit and try and this happened. I renamed my static folder to staticfiles and then in STATICFILES_DIRS replaced the path from
'/home/hussain/django/ratingsite/ratingsite/static/', to
'/home/hussain/django/ratingsite/ratingsite/staticfiles/', and it worked.
I have No Idea why. can someone please explain ?

output of python manage.py runserver --traceback

hussain@jarvis:~/django/ratingsite$ python manage.py runserver --traceback
Validating models...

0 errors found
December 08, 2013 - 01:48:39
Django version 1.5.4, using settings 'ratingsite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[08/Dec/2013 01:48:41] "GET / HTTP/1.1" 200 930
Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 73, in __call__
    return super(StaticFilesHandler, self).__call__(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 255, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
    return self.serve(request)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 56, in serve
    return serve(request, self.file_path(request.path), insecure=True)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/views.py", line 38, in serve
    absolute_path = finders.find(normalized_path)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 238, in find
    for finder in get_finders():
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 253, in get_finders
    yield get_finder(finder_path)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 31, in wrapper
    result = func(*args)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 275, in _get_finder
    return Finder()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 62, in __init__
    "The STATICFILES_DIRS setting should "
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
[08/Dec/2013 01:48:42] "GET /static/css/default.css HTTP/1.1" 500 59
Traceback (most recent call last):
Traceback (most recent call last):
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
  File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
    self.result = application(self.environ, self.start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 73, in __call__
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 73, in __call__
    return super(StaticFilesHandler, self).__call__(environ, start_response)
    return super(StaticFilesHandler, self).__call__(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 255, in __call__
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 255, in __call__
    response = self.get_response(request)
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
    return self.serve(request)
    return self.serve(request)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 56, in serve
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 56, in serve
    return serve(request, self.file_path(request.path), insecure=True)
    return serve(request, self.file_path(request.path), insecure=True)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/views.py", line 38, in serve
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/views.py", line 38, in serve
    absolute_path = finders.find(normalized_path)
    absolute_path = finders.find(normalized_path)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 238, in find
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 238, in find
    for finder in get_finders():
    for finder in get_finders():
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 253, in get_finders
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 253, in get_finders
    yield get_finder(finder_path)
    yield get_finder(finder_path)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 31, in wrapper
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 31, in wrapper
    result = func(*args)
    result = func(*args)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 275, in _get_finder
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 275, in _get_finder
    return Finder()
    return Finder()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 62, in __init__
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 62, in __init__
    "The STATICFILES_DIRS setting should "
    "The STATICFILES_DIRS setting should "
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting

Upvotes: 0

Views: 1005

Answers (2)

Williams
Williams

Reputation: 4318

This makes sense now.

The problem is this as is written in your stacktrace:

ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting

And per your initial configuration:

STATIC_ROOT = '/home/hussain/django/ratingsite/ratingsite/static'

STATICFILES_DIRS = (
   '/home/hussain/django/ratingsite/ratingsite/static',)

You will not that both of these point to exactly the same directory. This is not allowed for the reason described below.

The ImproperlyConfigured error was fixed when you changed the location of one of these configurations (so they were no longer identical).

The solution here is to:

  • create a different STATIC_ROOT directory (we'd call this something like /public/static/) do not put any files in this directory this is where all of the static files are automatically collected to when your site is ready to publish.

  • put the location/s of the files that you are developing in the STATICFILES_DIRS.

When you are ready to publish your project you are able to run $ ./manage.py collectstatic which will go through all the apps and staticfiles directories your project uses and compile a set of all the "final" static files you need in the STATIC_ROOT directory ready for publication with your finished site.

If your STATIC_ROOT directory was included in the directories that was gone through by collectstatic there would obviously be a recursive loop.

Upvotes: 2

Williams
Williams

Reputation: 4318

Have you added the following to your urls.py?

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Per the instructions here: https://docs.djangoproject.com/en/dev/howto/static-files/


Although you are getting a 500 error which suggests another problem.

What is the output of?:

$ python manage.py validate --traceback

Does this state some other kind of error?


How about the output of:

$ python manage.py shell

>>> from django.conf import settings
>>> print(settings.STATICFILES_DIRS)

As it is returning 500 rather than the expected 404 you could run:

$ python manage.py runserver --traceback

... and then see what the traceback history returns when you load the webpage.

Upvotes: 0

Related Questions