簡體   English   中英

Django靜態路徑未正確設置

[英]Django static path is not properly set

我正在嘗試學習Django ,在settings.py中我設置了:

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',)

我的css文件在我的

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

但是當我嘗試訪問網頁時,它不會加載CSS並顯示錯誤消息。

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

我是新來的,所以我不明白我的根目錄是什么。 什么先運行以及Django如何嘗試查找資源。 如果有人可以指導我做到這一點,那就太好了。


Guyz Guyz我只是在嘗試並嘗試,所以發生了這種情況。 我將我的static文件夾重命名為staticfiles ,然后在STATICFILES_DIRS替換了
'/home/hussain/django/ratingsite/ratingsite/static/',
'/home/hussain/django/ratingsite/ratingsite/staticfiles/',並且它起作用了。
我不知道為什么。 有人可以解釋嗎?

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 

現在這很有意義。

問題是這是在您的堆棧跟蹤中寫的:

ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting

並根據您的初始配置:

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

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

您不會希望它們都指向完全相同的目錄 由於以下原因,不允許這樣做。

當您更改這些配置之一的位置時, ImproperlyConfigured錯誤已修復(因此它們不再相同)。

解決方案是:

  • 創建一個不同的STATIC_ROOT目錄(我們將其稱為/public/static/ ), 不要在該目錄中放置任何文件,這是當您准備發布網站時所有靜態文件自動收集到的位置。

  • 將要開發的文件的位置放在STATICFILES_DIRS

當你准備發布你的項目,你可以運行$ ./manage.py collectstatic這將通過所有項目使用的應用程序和staticfiles目錄和編制一套所有的“最終”靜態文件的您在需要STATIC_ROOT目錄准備好與最終站點一起發布。

如果您STATIC_ROOT目錄被包含在通過過去了目錄collectstatic有顯然是一個遞歸循環。

您是否已將以下內容添加到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)

按照此處的說明: https : //docs.djangoproject.com/en/dev/howto/static-files/


盡管您遇到500錯誤,這提示了另一個問題。

輸出是什么?:

$ python manage.py validate --traceback

這會說明其他錯誤嗎?


如何輸出:

$ python manage.py shell

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

當它返回500而不是預期的404您可以運行:

$ python manage.py runserver --traceback

...,然后查看在加載網頁時返回的追溯歷史記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM