Django:管理员页面上没有CSS

时间:2017-02-02 14:47:35

标签: python django

在阅读了有关SO的一些教程和问题后(例如:css not loading in my login and admin page in django),我找不到加载css样式表的方法。

它来自我的MacOSX localhost计算机和我的新Ubuntu远程服务器之间的迁移。

我的管理员登录页面如下所示:

enter image description here

如您所见,我没有从/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/

考虑的CSS

我的所有管理部分都是这样的:没有CSS

我有一个 settings.py 文件,如下所示:

# 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'

所以我找不到办法来获得这种模板:

enter image description here

如果您有可以帮助我的建议?

提前谢谢你!

编辑:

来自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

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>

1 个答案:

答案 0 :(得分:1)

要运行collect static命令,我们需要设置STATIC_ROOT,其中将存储collectstatic输出文件,因此在您的设置文件中添加STATIC_ROOT并确保从您的apache conf提供STATIC_ROOT路径。 在您的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/更新为

Alias /static/ /var/www/html/myproject/staticfiles/

有关静态文件服务检查https://docs.djangoproject.com/en/1.10/howto/static-files/

的更多信息