I'm struggling getting staticfiles (CSS) to work on my Django site deployed to Openshift. I can workaround this by putting style info in the HTML templates, but the Django admin site stylesheets still will not load. Everything works locally, including with debug off.
Openshift stores staticfilesunder repo/ -> wsgi/ -> static/, where the main project directory is also under wsgi/.
Here are the relevant parts of settings.py:
ON_OPENSHIFT = True if 'OPENSHIFT_REPO_DIR' in os.environ else False
PROJECT_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir)
if ON_OPENSHIFT:
STATIC_ROOT = os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'static')
else:
STATIC_ROOT = ''
STATIC_URL = '/static/'
if ON_OPENSHIFT:
STATICFILES_DIRS = (os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'static'))
else:
STATICFILES_DIRS = [os.path.join(PROJECT_DIR, 'static')]
I have a script that runs the following when deploying:
cd $OPENSHIFT_REPO_DIR/wsgi/squadron
python manage.py collectstatic --noinput
I'm using Python 3.3 and Django 1.6, and referencing the following guides: 1 and 2 from the docs.
What am I doing wrong? I don't get an error message, just no stylesheets, either my own, or on Django admin. When I use SCP, I can verify my stylesheet is in the correct Openshift directory.
This is the best source to read when you are trying to get OpenShift working with Django.
http://appsembler.com/blog/django-deployment-using-openshift/
Some of this will be changing (for the better) with upcoming releases but this should help for now.
Related
I deployed my Django app on server with Dokku. Staticfiles seems to be working as CSS is perfectly fine in admin dashboard, I can upload file but I cannot reach the file.
I discovered that I should be using Dokku persistence storage anyway. So I set it up (I suppose). Then I setup my Nginx. Before Nginx, Django was showing "page not found" for the file path. Now, I get "The page you were looking for doesn't exist.".
How can I correctly connect my app to persistence storage?
Edit:
I manually added a file to /var/lib/dokku/data/storage/dokkuappname and was able to download. So Nginx is working but Django is not using storage folder as media folder.
Static and media settings on Django
# STATIC
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR / "staticfiles")
# https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = "/static/"
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [str(APPS_DIR / "static")]
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
# MEDIA
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(APPS_DIR / "media")
# https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = "/media/"
Nginx media.conf settings
location /media/ {
alias /var/lib/dokku/data/storage/dokkuappname/;
}
dokku storage:list dokkuappname result
dokkuappname volume bind-mounts:
/var/lib/dokku/data/storage/dokkuappname:/media
Solution
I wanted to see the path to Django's media folder. I put print(MEDIA_ROOT) in base.py to print the path during deployment. It was /app/my_app_dir/media. I realized that /app existed in Dokku documentation, but I always thought that it was a place holder like path/to/app. No, it was the path.
Dokku mount example
dokku storage:mount app-name /var/lib/dokku/data/storage/node-js-app:/app/storage
I fixed my mount command:
dokku storage:mount my_app/var/lib/dokku/data/storage/my_app:/app/my_app_dir/media
and it finally worked!
I created an webapp with Django, heroku and S3.
In production it seems that the upload of static files are only working when comment out django_heroku.settings(locals())
#django_heroku.settings(locals())
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Then in the HTML, the urls to the static files do either get an encryption in the name like "main.css" becomes "main-5354332324.css", and cant get access, or I add
AWS_DEFAULT_ACL = "public-read"
AWS_QUERYSTRING_AUTH = False
and the names of the static file stay but still can't be accessed.
So far I understand that django_heroku.settings(locals()) would overwrite locations with a default value. I was wondering what exactly happens here and what is an appropriates solution for production, as django_heroku.settings(locals()) should be actually at the end and it should be AWS_DEFAULT_ACL=None as far as I know.
hey I had the same problem django_heroku overwrites whatever you have using whitenoise and saving it to staticfiles what i did was add this line at the bottom of my code django_heroku.settings(locals(), staticfiles=False) add your config vars to heroku either from the cli or app settings
I had faced the same problem, when I put django_heroku.settings(locals()) in settings.py module. It worked after modifying the line to django_heroku.settings(locals(), staticfiles=False).
I am using django 1.3 and trying to deploy a django project (client sent) on my dev machine (ubuntu 12.04). The problem is regarding the static files. My directory structure is as follows:
project_name
media
static
css
img
js
settings.py
Here is my settings.py:
ROOT = '/home/user/project_name'
MEDIA_ROOT = '%s/media/' % ROOT
MEDIA_URL = '/media/'
STATIC_ROOT = '%s/static/' % ROOT
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
My site is perfectly deployed but the css, js and imgs are missing. Same is the case for the admin interface. When I use the link http://mysite.com/static/js/some.js it gives a 404.
Help would be appreciated and up-voting an answer is custom.
I think you need to run ./manage.py collectstatic :)
You don't mention configuring your web server to actually serve the static files. You need to point it at the directory that collectstatic put them into.
did you run python manage.py collectstatic ? see here
be careful that in production you should place the static file in a static server. There should be something in the guidelines.
And a little offtopic..
It will be better, to use:
MEDIA_ROOT = os.path.join(os.path.dirname(file),'media').replace('\','/')
STATIC_ROOT = os.path.join(os.path.dirname(file),'static').replace('\','/')
and in main urls.py at development, django webserver only:
urlpatterns = patterns('',
(r'^media/(?P.*)','django.views.static.serve',{'document_root': os.path.join(os.path.dirname(file),'media').replace('\','/') }),
(r'^static/(?P.*)','django.views.static.serve',{'document_root': os.path.join(os.path.dirname(file),'static').replace('\','/')}),
In this way, u dont need collectstatic, ull need it at production server, where u will use nginx or something other to server your static
I'm trying to use django-cumulus for serving files off Rackspace CloudFiles. I'm currently only trying it on my local dev server, using Django 1.4.2.
I can use cumulus's syncstatic management command to upload all my static assets successfully, but I can't seem to display them on my site with the same settings.
If my relevant settings are:
STATIC_URL = '/static/'
CUMULUS = {
'USERNAME': 'myusername',
'API_KEY': 'myapikey',
'CONTAINER': 'mycontainername',
'STATIC_CONTAINER': 'mycontainername',
}
DEFAULT_FILE_STORAGE = 'cumulus.storage.CloudFilesStorage'
STATICFILES_STORAGE = 'cumulus.storage.CloudFilesStaticStorage'
then when I run syncstatic all my apps' static files are uploaded into /mycontainername/static/, as I'd expect. But when I load a page in admin it ignores STATIC_URL and tries to serve assets from URLs like http://uniquekey....r82.cf2.rackcdn.com/path/to/file.css rather than http://uniquekey....r82.cf2.rackcdn.com/static/path/to/file.css.
Also, I can't see how to have my public (non-admin) pages use the static files on CloudFiles, rather than serving them from a local /static/ directory.
Have I missed some crucial setting, or am I doing something else wrong?
I had the same problem. What i did was to
git clone https://github.com/richleland/django-cumulus.git
edit context_processors.py
from django.conf import settings
from cumulus.storage import CloudFilesStorage
def cdn_url(request):
"""
A context processor to expose the full cdn url in templates.
"""
cloudfiles_storage = CloudFilesStorage()
static_url = '/'
container_url = cloudfiles_storage._get_container_url()
cdn_url = container_url + static_url
print {'CDN_URL': cdn_url}
return {'CDN_URL': cdn_url}
Once you are done, install it with sudo python setup.py install
Do note that context_processors.py from django cumulus is actually quite slow
How can I view my static css files? I've set my STATIC_ROOT, and am using python manage.py runserver.
In my development environment, according the docs, I only need to place my static files (in this case, /static/css/typography.css) in my STATIC_ROOT, and python manage.py runserver will automatic create the views necessary to access it if I have DEBUG = True.
STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static")
I've also tried manually adding the views in URLConf, which won't display the css file either:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
In my template, the {{ STATIC_URL }} gets to the correct address (/static/css/typography.css), but it will not serve the file when I try to access it:
<link href="{{ STATIC_URL }}css/typography.css" rel="stylesheet" type="text/css">
Notes: The other Django related static files questions on StackOverflow are over two years old. Django version 1.3b1 differentiates STATIC (static files, such as css and images) and MEDIA (user-uploaded file).
Besides. all the tries mentioned above, you must also make sure that your template is receiving the RequestContext when called from the view.
http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/
This link gives various ways of doing the same and you could choose any one. :) Also, the TEMPLATE_CONTEXT_PROCESSORS must be added to settings.py for this to take effect.
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"myapp.processor.foos",
)
Note: STATIC_ROOT is the place where all your static files are stored by Django after collecting them from STATICFILES_DIRS.
Runserver will pick them up from the path mentioned in STATIC_ROOT, so STATIC_URL should point to the same location as STATIC_ROOT.
Answered here: Django staticfiles app help
I was putting my files in STATIC_ROOT, so adding this works:
STATICFILES_DIRS = (STATIC_ROOT,)
In development:
#settings.py have this by default
STATIC_URL = '/static/'
This means when you want to use static files, django will look for them in folder 'static' in your app directory. So you need to create folder 'static' in every your app and put inside your static files.
In production:
1. Make place to hold static files from all app's
STATIC_ROOT = "/var/www/example.com/static/"
2. Run python manage.py collectstatic to collect static files from all app's in STATIC_ROOT folder.
3. In settings.py change DEBUG=False and ALLOWED_HOSTS = ['yourhostadress'].
4. Point your webserver to STATIC_ROOT folder (eg. for Apache2.4):
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
After this setup, your django project should be able to run (using mod_wsgi) and use static files on Apache web server.