0

我是 Django 新手,管理区域的 css 没有呈现。首先,我创建了一个加载了管理模块的应用程序,这就是我所做的:

通过将调试模式更改为 false 并添加数据库凭据来配置 setting.py。然后:

manage.py syncdb

Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

-

manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Copying '/Library/Python/2.7/site-packages/django/contrib/admin/static/admin/css/base.css'
Copying '/Library/Python/2.7/site-packages/django/contrib/admin/static/admin/css/changelists.css'
Copying '/Library/Python/2.7/site-packages/django/contrib/admin/static/admin/css/dashboard.css'
Copying '/Library/Python/2.7/site-packages/django/contrib/admin/static/admin/css/forms.css'
Copying '/Library/Python/2.7/site-packages/django/contrib/admin/static/admin/css/ie.css'
Copying '/Library/Python/2.7/site-packages/django/contrib/admin/static/admin/css/login.css'
Copying '/Library/Python/2.7/site-packages/django/contrib/admin/static/admin/css/rtl.css'

(...)

当我启动服务器时我得到了什么

Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[09/Feb/2013 13:57:53] "GET /admin/ HTTP/1.1" 200 3270
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
  File "/Library/Python/2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
  File "/Library/Python/2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)
    response = self.get_response(request)
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 153, in get_response
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 153, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
    return callback(request, **param_dict)

主页给出“发生服务器错误。请联系管理员。”,我可以访问 site.com/admin/ 但没有 css。项目文件夹如下所示:

--project
----manage.py
----admin
-------css
-------img
-------js
----project
-------setting.py
-------urls.py
-------wsgi.py
-------__init__.py
4

1 回答 1

1

首先,如果您正在运行开发服务器(带有python manage.py runserver),那么您应该将您的DEBUG设置为 true。此外,您的STATIC_ROOT设置似乎尚未设置(或设置为项目的根目录 - 当您在生产中运行应用程序时,这不是您想要的)。

在您的 settings.py 文件中,您需要确保您已django.contrib.staticfiles包含在INSTALLED_APPS设置中。然后确保你已经定义了 STATIC_URL (我认为它应该默认为/static/)。假设您在 django 的开发服务器上使用 django 1.4,那么一切都应该可以正常工作而无需运行collectstatic.

查看有关提供静态文件的文档。值得注意的是,在生产服务器上提供静态文件的过程是不同的。

于 2013-02-09T23:34:48.773 回答