简体   繁体   English

Django静态路径未正确设置

[英]Django static path is not properly set

I am trying to learn Django , In settings.py I have 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',)

I have my css files in my 我的css文件在我的

/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. 但是当我尝试访问网页时,它不会加载CSS并显示错误消息。

"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. 什么先运行以及Django如何尝试查找资源。 If someone can guide me through this it would be great. 如果有人可以指导我做到这一点,那就太好了。


Guyz Guyz I was just doing hit and try and this happened. Guyz Guyz我只是在尝试并尝试,所以发生了这种情况。 I renamed my static folder to staticfiles and then in STATICFILES_DIRS replaced the path from 我将我的static文件夹重命名为staticfiles ,然后在STATICFILES_DIRS替换了
'/home/hussain/django/ratingsite/ratingsite/static/', to '/home/hussain/django/ratingsite/ratingsite/static/',
'/home/hussain/django/ratingsite/ratingsite/staticfiles/', and it worked. '/home/hussain/django/ratingsite/ratingsite/staticfiles/',并且它起作用了。
I have No Idea why. 我不知道为什么。 can someone please explain ? 有人可以解释吗?

output of python manage.py runserver --traceback 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 

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). 当您更改这些配置之一的位置时, ImproperlyConfigured错误已修复(因此它们不再相同)。

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. 创建一个不同的STATIC_ROOT目录(我们将其称为/public/static/ ), 不要在该目录中放置任何文件,这是当您准备发布网站时所有静态文件自动收集到的位置。

  • put the location/s of the files that you are developing in the STATICFILES_DIRS . 将要开发的文件的位置放在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. 当你准备发布你的项目,你可以运行$ ./manage.py collectstatic这将通过所有项目使用的应用程序和staticfiles目录和编制一套所有的“最终”静态文件的您在需要STATIC_ROOT目录准备好与最终站点一起发布。

If your STATIC_ROOT directory was included in the directories that was gone through by collectstatic there would obviously be a recursive loop. 如果您STATIC_ROOT目录被包含在通过过去了目录collectstatic有显然是一个递归循环。

Have you added the following to your urls.py ? 您是否已将以下内容添加到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/ 按照此处的说明: https : //docs.djangoproject.com/en/dev/howto/static-files/


Although you are getting a 500 error which suggests another problem. 尽管您遇到500错误,这提示了另一个问题。

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: 当它返回500而不是预期的404您可以运行:

$ python manage.py runserver --traceback

... and then see what the traceback history returns when you load the webpage. ...,然后查看在加载网页时返回的追溯历史记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM