为什么我的 django 管理站点没有 css 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4420378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why my django admin site does not have the css style
提问by zjm1126
I make a Django admin site using Django development version
我使用Django 开发版本制作了一个 Django 管理站点
But it does not have a CSS style :
但它没有 CSS 样式:
what can I do?
我能做什么?
thanks
谢谢
采纳答案by Evan Porter
Django does not serve static files on it's own. You have to tell it where the files are.
Django 本身不提供静态文件。你必须告诉它文件在哪里。
The ADMIN_MEDIA_PREFIX in the settings.pywill point Django in the right location.
将在settings.py ADMIN_MEDIA_PREFIX将在正确的位置指向Django的。
Since you're using the development version, you'll want the dev-specific document for static files how-to. Adam's link will lead you to the 1.2 version.
由于您使用的是开发版本,因此您需要针对静态文件 how-to的开发特定文档。Adam 的链接将引导您进入 1.2 版本。
回答by David Mann
After setting up your STATIC_ROOT
and STATIC_URL
, you may have to run
设置好STATIC_ROOT
and 后STATIC_URL
,您可能需要运行
python manage.py collectstatic
回答by Shamar
ADMIN_MEDIA_PREFIX
is deprecated now, use STATIC_URL
instead. Setting STATIC_URL = '/static/'
in settings.py should do the job. Try:
ADMIN_MEDIA_PREFIX
现在已弃用,请STATIC_URL
改用。设置STATIC_URL = '/static/'
在settings.py应该做的工作。尝试:
import os.path import sys
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
and then:
进而:
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
Works on Django 1.4 pre-alpha SVN-16920.
适用于 Django 1.4 pre-alpha SVN-16920。
回答by Chris SH
I ran into this issue as well following the Django Book Tutorial. In Chapter 5|Installing the model, the book states when referring to the default INSTALLED_APPS- "Temporarily comment out all six of those strings by putting a hash character (#) in front of them." http://www.djangobook.com/en/2.0/chapter05.html
我在 Django Book Tutorial 之后也遇到了这个问题。在第 5 章|安装模型中,该书在提到默认的 INSTALLED_APPS 时指出-“通过在它们前面放置一个哈希字符 (#) 来临时注释掉所有六个这些字符串。” http://www.djangobook.com/en/2.0/chapter05.html
Then, in Chapter 6, the Book tells the reader to uncomment 4 of those 6 lines- "note that we commented out these four INSTALLED_APPS entries in Chapter 5. Uncomment them now."
然后,在第 6 章,本书告诉读者取消注释这 6 行中的 4 行——“请注意,我们在第 5 章中注释掉了这四个 INSTALLED_APPS 条目。现在取消注释它们。”
But the statcifiles line is what is needed to restore CSS to the admin page, so uncomment that 'django.contrib.staticfiles',
但是 statcifiles 行是将 CSS 恢复到管理页面所需的行,因此取消注释“django.contrib.staticfiles”,
回答by Taylor
I read several other threads trying to fix this...resorted to an alias as in other threads. This assumes that your own custom app is serving static files correctly, which would indicate that your STATIC_ROOT and STATIC_URL have proper settings.
我阅读了其他几个试图解决这个问题的线程......像在其他线程中一样使用别名。这假设您自己的自定义应用程序正确提供静态文件,这表明您的 STATIC_ROOT 和 STATIC_URL 具有正确的设置。
STATIC_ROOT = ''
STATIC_URL = '/static/'
Then (from your static directory):
然后(从您的静态目录):
ubuntu@ip-1-2-3-4:/srv/www/mysite.com/app_folder/static$ sudo ln -s /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/ admin
Hope this helps someone...there are a lot of threads on this topic. :(
希望这对某人有所帮助...有很多关于这个主题的主题。:(
回答by Brad P.
In /project_name/project_name/settings.py
you need to set STATIC_URL
to tell your site what url to use for static files.
在/project_name/project_name/settings.py
您需要设置STATIC_URL
以告诉您的站点用于静态文件的 url。
Then set STATIC_ROOT
to be some folder on your filesystem that is not the same as any of your directories listed in STATICFILES_DIRS
list.
然后设置STATIC_ROOT
为文件系统上的某个文件夹,该文件夹与STATICFILES_DIRS
列表中列出的任何目录都不相同。
Once STATICFILES_ROOT
is set, you would run python manage.py collectstatic
from the project directory.
一旦STATICFILES_ROOT
设置,你可以运行python manage.py collectstatic
从项目目录。
This will copy all the admin static files and all files in any other folders listed in the STATICFILES_DIRS
list. Basically this puts all your static files in one place so you you can move them to your CDN when deploying your site. If you are like me and don't have a CDN, then you have two options:
这将复制STATICFILES_DIRS
列表中列出的任何其他文件夹中的所有管理静态文件和所有文件。基本上,这会将您所有的静态文件放在一个地方,这样您就可以在部署站点时将它们移动到 CDN。如果你像我一样没有 CDN,那么你有两个选择:
- Add the folder you set as
STATIC_ROOT
to theSTATICFILES_DIRS
list. This will allow the staticfiles finders in django to locate all the static files. - Move the entire folder of static files somewhere else on your file system and direct
STATICFILES_DIRS
to include that new location.
- 将您设置的文件夹添加
STATIC_ROOT
到STATICFILES_DIRS
列表中。这将允许 Django 中的静态文件查找器定位所有静态文件。 - 将整个静态文件文件夹移动到文件系统上的其他
STATICFILES_DIRS
位置,并直接包含该新位置。
I make no comments about security with this answer, it is just the way I have been able to develop with my web server for small projects. I expect that you will want a CDN as django suggest if you are doing anything larger scale.
我对这个答案的安全性没有发表任何评论,这只是我能够使用我的 Web 服务器为小型项目进行开发的方式。如果您正在做任何更大规模的事情,我希望您会像 django 建议的那样需要 CDN。
UPDATE:
I just ran into this issue and this method didn't quite do what I think you want. What ended up working for me was after I ran collectstatic
I just copied the admin static files that it put into STATICFILES_ROOT
into the directory that I had used for my own static files. That solved the issue for me.
更新:我刚刚遇到了这个问题,这个方法并没有完全按照我的想法去做。最终对我有用的是在我运行之后,collectstatic
我只是将它放入的管理静态文件复制STATICFILES_ROOT
到我用于我自己的静态文件的目录中。那为我解决了这个问题。
回答by cod3monk3y
In addition to many of the other answers being useful, I had a problem that hasn't yet been noted. After upgrading from Django 1.3 to 1.6, my static files directory had a broken symbolic link to the django admin static files.
除了许多其他有用的答案之外,我还有一个尚未注意到的问题。从 Django 1.3 升级到 1.6 后,我的静态文件目录有一个到 django 管理静态文件的损坏的符号链接。
My settings.py was configured with:
我的 settings.py 配置为:
STATICFILES_DIRS = (
'/var/www/static/my-dev',
)
According to this answer,
根据这个答案,
Django will now expect to find the admin static files under the URL /admin/.
Django 现在期望在 URL /admin/ 下找到管理静态文件。
I had a symbolic link /var/www/static/my-dev/admin
which was set to:
我有一个符号链接/var/www/static/my-dev/admin
,它被设置为:
admin -> /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/
That location no longer exists in django 1.6, so I updated the link to:
该位置在 django 1.6 中不再存在,因此我将链接更新为:
admin -> /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/
And now my admin site is working properly.
现在我的管理站点工作正常。
回答by Gio
While following the Django tutorial, I had a similar problem and in my case the issue was the mimetype used by the development server when serving css files.
在遵循 Django 教程时,我遇到了类似的问题,在我的情况下,问题是开发服务器在提供 css 文件时使用的 mimetype。
The mimetype served was 'application/x-css' which led to following warning message in Chrome (in the 'Network' tab of the Developer tools):
提供的 mimetype 是“application/x-css”,这导致 Chrome 中出现以下警告消息(在开发人员工具的“网络”选项卡中):
Resource interpreted as Stylesheet but transferred with MIME type application/x-css: "http://127.0.0.1:8000/static/admin/css/base.css"
资源被解释为样式表,但使用 MIME 类型 application/x-css 传输:“ http://127.0.0.1:8000/static/admin/css/base.css”
The workaround that I've found: changing the mimetype to be served by adding following lines to the django webapp's manage.py file:
我发现的解决方法:通过将以下几行添加到 django webapp 的 manage.py 文件来更改要提供的 mimetype:
import mimetypes
mimetypes.init()
mimetypes.types_map['.css'] = 'text/css'
Note: worked for me with Django 1.7.4 on Python 2.7 and Chrome 40.0
注意:在 Python 2.7 和 Chrome 40.0 上使用 Django 1.7.4 为我工作
回答by Khateeb321
My issue was resolved by creating new Virtual Environment for the project, before that I was using general system level python interpreter.
我的问题是通过为项目创建新的虚拟环境解决的,在此之前我使用的是通用系统级 python 解释器。
$ mkvirtualenv myproject
Reference: https://docs.djangoproject.com/en/2.1/howto/windows/
回答by Yulun
If you are using Apache server to host your django site, you need to make sure the static alias point to your /directory to site/site_media/static/. If your static files are in /directory to site/site/site_media/static/, the previous Apache alias configuration will not work.
如果您使用 Apache 服务器来托管您的 django 站点,您需要确保静态别名指向您的 /directory 到 site/site_media/static/。如果您的静态文件在 /directory 到 site/site/site_media/static/,之前的 Apache 别名配置将不起作用。