Reputation: 6128
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>
Upvotes: 0
Views: 5586
Reputation: 1630
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/
Upvotes: 3