简体   繁体   English

渲染管理员静态文件-DJANGO

[英]Rendering admin static files - DJANGO

I'm new to Django and the css for the admin area are not rendered. 我是Django的新手,未呈现admin区域的css。 first, i created an app with admin module loaded and thats what i did : 首先,我创建了一个加载了管理模块的应用,这就是我所做的:

configured the setting.py by changing the debug mode to false and added the database credential. 通过将调试模式更改为false来配置setting.py,并添加了数据库凭据。 then: 然后:

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'

(...) (...)

what i get when i start the server 启动服务器后会得到什么

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)

the home page gives a "A server error occurred. Please contact the administrator.", i can access the site.com/admin/ but without css. 主页上显示“发生服务器错误。请与管理员联系。”,我可以访问site.com/admin/,但没有CSS。 The project folder looks like this : 项目文件夹如下所示:

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

Firstly if you're running the development server (with python manage.py runserver ), then you should have your DEBUG set to true. 首先,如果您正在运行开发服务器(使用python manage.py runserver ),则应将DEBUG设置为true。 Also it looks like your STATIC_ROOT setting hasn't been set (or is set to the root of your project - this isn't what you want when you come to run your application in production). 同样,看起来您的STATIC_ROOT设置尚未设置(或设置为项目的根目录-这不是在生产环境中运行应用程序时想要的设置)。

In your settings.py file you need to make sure you have django.contrib.staticfiles included in the INSTALLED_APPS setting. 在settings.py文件中,需要确保INSTALLED_APPS设置中包含django.contrib.staticfiles文件。 Then make sure you've got STATIC_URL defined (I think it should default to /static/ ). 然后确保已定义STATIC_URL(我认为它应该默认为/static/ )。 Assuming you're using django 1.4 on django's dev server, everything should just work without needing to run collectstatic . 假设您在django的dev服务器上使用django 1.4,一切都应该可以正常运行,而无需运行collectstatic

Have a look at the docs on serving static files . 看看有关提供静态文件的文档 It's worth noting that the process for serving static files is different when you do it on a production server. 值得注意的是,在生产服务器上处理静态文件的过程有所不同。

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

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