Python Django静态路径设置不正确
我正在努力学习Django, 在settings.py中,我设置了:Python Django静态路径设置不正确,python,css,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/hus
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如何尝试查找资源。如果有人能引导我度过这一关,那就太好了
哥们,哥们,我只是想尝试一下,事情就这样发生了。我将我的
static
文件夹重命名为staticfiles
,然后在staticfiles\u DIRS
中替换了'/home/hussain/django/ratingsite/ratingsite/static/',
到'/home/hussain/django/ratingsite/ratingsite/staticfiles/',
的路径,它成功了<我不知道为什么。有人能解释一下吗
python manage.py runserver的输出——回溯
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
您是否已将以下内容添加到url.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)
按照此处的说明:
尽管您得到了一个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
。。。然后查看加载网页时回溯历史记录返回的内容。这现在是有意义的
问题是,正如stacktrace中所述:
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',)
您不会发现这两个目录都指向完全相同的目录。由于下述原因,这是不允许的
当您更改其中一个配置的位置时,配置不当
错误已修复(因此它们不再相同)
这里的解决方案是:
- 创建一个不同的
STATIC\u ROOT
目录(我们称之为类似/public/STATIC/
)不要将任何文件放在该目录中。当您的站点准备发布时,所有静态文件都会自动收集到该目录中
- 将您正在开发的文件的位置放在
静态文件\u DIRS
中
当您准备发布项目时,您可以运行$./manage.py collectstatic
,它将遍历项目使用的所有应用程序和静态文件目录,并在static\u ROOT
目录中编译一组您需要的“最终”静态文件,以便与完成的站点一起发布
如果您的STATIC\u ROOT
目录包含在通过collectstatic
的目录中,显然会有一个递归循环。在STATICFILES\u DIRS
中指定的路径实际上被用作静态文件的输入位置,即当您运行collectstatic
时,文件将从其中提取到STATIC\u ROOT
的路径。在你的情况下,他们都是一样的。@AshwiniChaudhary那么我错在哪里呢?为什么它不工作?您的系统上的default.css
的位置是什么?您使用的是哪个版本的Django?这可能因版本而异。@AshwiniChaudhary/home/hussain/django/ratingsite/ratingsite/static/css/default.cssYes确实添加了静态URL和文档根,并且$python manage.py validate--traceback
给出了发现的0个错误('/home/hussain/django/ratingsite/ratingsite/ratingsite/static/'))您能否确认仍然收到“500内部服务器错误”
”。。。如果您的静态设置有问题,这将返回一个“404
”。是的,它给出了”网络错误:500内部服务器错误-http://localhost:8000/static/css/default.css“
谢谢。你帮了很多忙。这就是我想要的答案,一个解释为什么它不起作用。谢谢你能给我介绍一些关于Django的好教程吗。我试着阅读官方教程和djnagobooks,但对于这样的问题不是很清楚。