Django:管理页面上没有CSS [英] Django : no CSS on Admin page
问题描述
在阅读有关SO的一些教程和问题之后(例如:
如您所见,我没有从/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/
<
我所有的管理部分看起来都像这样:没有CSS
我有一个 settings.py 文件,看起来像:
#像这样在项目内部构建路径:os.path.join(BASE_DIR,...)BASE_DIR = os.path.dirname(os.path.abspath(__ file__))TEMPLATE_DIRS =(os.path.join(BASE_DIR,'模板'),)#应用定义INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','app1','app2','app3','app4','app5','django_countries','app6','app7',]MIDDLEWARE_CLASSES = ['django.contrib.sessions.middleware.SessionMiddleware','django.middleware.locale.LocaleMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.middleware.gzip.GZipMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.auth.middleware.SessionAuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware','django.middleware.security.SecurityMiddleware',]模板= [{'BACKEND':'django.template.backends.django.DjangoTemplates','DIRS':['/var/www/html/myproject/templates/','/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/','/var/www/html/myproject/app1/templates/','/var/www/html/myproject/app2/templates/','/var/www/html/myproject/app3/templates/'],'APP_DIRS':是的,'选项': {'debug':调试,'context_processors':['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},},]#静态文件(CSS,JavaScript,图像)#https://docs.djangoproject.com/zh/1.10/howto/static-files/STATIC_URL ='/静态/'STATICFILES_DIRS =(os.path.join(BASE_DIR,"/var/www/html/myproject/static/"),)MEDIA_ROOT = os.path.join(BASE_DIR,'media')MEDIA_URL ='/media/'#会话年龄100分钟SESSION_COOKIE_AGE = 100 * 60#X * 60s#简单数学CAPTCHA_CHALLENGE_FUNCT ='captcha.helpers.math_challenge'
所以我找不到找到这种模板的方法:
如果您有对我有帮助的建议?
先谢谢您!
从 python manage.py collectstatic
Traceback(最近一次通话最近):< module>中的文件"manage.py",第22行.execute_from_command_line(sys.argv)文件"/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py",行367,在execute_from_command_line中utility.execute()在执行的文件"/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py"中,行359self.fetch_command(子命令).run_from_argv(self.argv)在run_from_argv中的第294行中,文件"/usr/local/lib/python2.7/dist-packages/django/core/management/base.py"self.execute(* args,** cmd_options)执行中的文件"/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",第345行输出= self.handle(* args,** options)句柄中的文件"/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py",第193行收集= self.collect()收集中的文件"/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py",第114行对于get_finders()中的finder:get_finders中的文件"/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py",第264行产生get_finder(finder_path)包装中的文件"/usr/local/lib/python2.7/dist-packages/django/utils/lru_cache.py",第100行结果= user_function(* args,** kwds)在get_finder中的文件"/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py",第277行返回Finder()__init__中的文件"/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py",第66行"STATICFILES_DIRS设置应为"django.core.exceptions.ImproperlyConfigured:STATICFILES_DIRS设置不应包含STATIC_ROOT设置
Apache2.conf的一部分:
别名/static//var/www/html/myproject/static/<目录/var/www/html/myproject/static/>要求所有授予</目录><目录/>选项FollowSymLinksAllowOverride无要求所有被拒绝</目录><目录/usr/share>AllowOverride无要求所有授予</目录>WSGIScriptAlias//var/www/html/myproject/myproject/wsgi.pyWSGIPythonPath/var/www/html/myproject<目录/var/www/html/myproject/myproject><文件wsgi.py>选项索引FollowSymLinks要求所有授予SetEnvIfNoCase主机.+ VALID_HOST拒绝订单,允许全部拒绝允许来自env = VALID_HOST</文件></目录>#<目录/srv/>#选项索引FollowSymLinks#AllowOverride无#要求所有授予#</Directory>LoadModule wsgi_module/usr/lib/apache2/modules/mod_wsgi.so< VirtualHost *:80>别名/media//usr/local/lib/python2.7/site-packages/django/contrib/admin/media/</VirtualHost>
要运行collect static命令,我们需要将STATIC_ROOT设置为将存储静态输出文件的位置,因此在您的设置文件中添加STATIC_ROOT并确保STATIC_ROOT路径已投放从您的apache conf.在您的settings.py
STATIC_URL ='/static/'STATICFILES_DIRS =(os.path.join(BASE_DIR,'static'),)STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
因此,当您运行collectstatic时,所有文件都将收集到staticfiles目录中.
并将 Alias/static/
更新为
别名/static//var/www/html/myproject/staticfiles/
有关静态文件服务的更多信息,请检查 https://docs.djangoproject.com/en/1.10/howto/static-files/
After reading some tutorials and questions on SO (for example : css not loading in my login and admin page in django), I don't find a way to load css stylesheet.
It comes from migration between my MacOSX localhost machine and my new Ubuntu distant server.
My admin login page looks like :
As you can see, I don't have CSS which is taken account from /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/
All my admin part looks like this : without CSS
I have a settings.py file which looks like :
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
'app2',
'app3',
'app4',
'app5',
'django_countries',
'app6',
'app7',
]
MIDDLEWARE_CLASSES = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.gzip.GZipMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/var/www/html/myproject/templates/',
'/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/',
'/var/www/html/myproject/app1/templates/',
'/var/www/html/myproject/app2/templates/',
'/var/www/html/myproject/app3/templates/' ],
'APP_DIRS': True,
'OPTIONS': {
'debug' : DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "/var/www/html/myproject/static/"),)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# SESSION AGE 100 Minutes
SESSION_COOKIE_AGE = 100*60 # X * 60s
# Simple Math
CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.math_challenge'
So I don't find a way to get this kind of template :
If you have advices which could help me ?
Thank you by advance !
EDIT :
Traceback from python manage.py collectstatic
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 193, in handle
collected = self.collect()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
for finder in get_finders():
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 264, in get_finders
yield get_finder(finder_path)
File "/usr/local/lib/python2.7/dist-packages/django/utils/lru_cache.py", line 100, in wrapper
result = user_function(*args, **kwds)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 277, in get_finder
return Finder()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 66, in __init__
"The STATICFILES_DIRS setting should "
django.core.exceptions.ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
Part of Apache2.conf :
Alias /static/ /var/www/html/myproject/static/
<Directory /var/www/html/myproject/static/>
Require all granted
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
WSGIScriptAlias / /var/www/html/myproject/myproject/wsgi.py
WSGIPythonPath /var/www/html/myproject
<Directory /var/www/html/myproject/myproject>
<Files wsgi.py>
Options Indexes FollowSymLinks
Require all granted
SetEnvIfNoCase Host .+ VALID_HOST
Order Deny,Allow
Deny from All
Allow from env=VALID_HOST
</Files>
</Directory>
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
<VirtualHost *:80>
Alias /media/ /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/
</VirtualHost>
To run the collect static command we need to set STATIC_ROOT where out collectstatic output files will be stored so add STATIC_ROOT in your settings file and make sure the STATIC_ROOT path served from your apache conf. In your settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
so when you run collectstatic all the files will be collected you staticfiles directory.
And update Alias /static/
to
Alias /static/ /var/www/html/myproject/staticfiles/
for more about static file serving check https://docs.djangoproject.com/en/1.10/howto/static-files/
这篇关于Django:管理页面上没有CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!