python - Django 静态路径设置不正确

标签 python css django

我正在尝试学习 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 它将遍历您的元素使用的所有应用程序和静态文件目录并编译一组所有STATIC_ROOT 目录中您需要的“最终”静态文件已准备好与完成的站点一起发布。

如果您的 STATIC_ROOT 目录包含在 collectstatic 遍历的目录中,则显然会出现递归循环。

关于python - Django 静态路径设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450865/

相关文章:

Javascript Prompt() 只能内联工作,不能在外部文件中工作

python - pytest 不会将日期字段转换为 Django 中的 datetime.date 对象

python - 子类构造函数返回父类(super class)对象

用于静态类型类属性的 Python 模块

python - 为什么python内置的sum(),max(),min()等函数可以用来计算numpy数据类型的ndarray?

javascript - 为重复的 div 打开图像预览

css - 覆盖特定 div 的 css?

python - 运行 Python 程序时出错 : com_error: (-2147023174, 'The RPC server is unavailable.' , None, None)

Python 按名称排序

mysql - Django 模型 : What is a efficient way to list Foreign Key?