How can I set up collectfast for django on heroku?
This is assuming I've already successfully set up static files hosting and serving from Amazon S3.
1) To disable heroku's automatic collectstatic, run:
heroku config:set DISABLE_COLLECTSTATIC=1
2) Add the following to settings.py to use a table in your database for the collectfast's caching. Commit and push the change to heroku.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
},
'collectfast': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'collectfast_cache',
'TIMEOUT': 60,
'OPTIONS': {
'MAX_ENTRIES': 10000
},
},
}
COLLECTFAST_CACHE = 'collectfast'
4) To create the required cache table in the database, run:
heroku run createcachetable
5) To restore heroku's automatic collectstatic, run:
heroku config:unset DISABLE_COLLECTSTATIC
Each deploy will now correctly use collectfast to collect modified static files to s3.
Related
I configured ElastiCache redis with cluster mode enabled.
I want to connect ElastiCache with local Django.
So I configured bastion host.
I connected ElastiCache(non-cluster mode) with local Django. I tried cache.set(), cache.get(). It's OK.
I installed 'Django-redis-cache' and 'settings.py' is like that.
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': 'localhost:6379',
}
}
But I have problem when I connect ElastiCache(cluster mode) with django.
I tried tunneling with ElastiCache configuration endpoint.
When I use the same 'settings.py', error message is like that.
'SELECT is not allowed in cluster mode'
So, I changed 'settings.py'.
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': 'localhost:6379',
'OPTIONS': {
'DB': 0
},
}
}
And then, error message is like that.
'MOVED 4205 xx.xx.xx.xxx:6379'
What I have to do?
There are no example which connect ElastiCache(cluster mode) with Django.
I have configured cache in settings.py in my Django project as follows:
CACHE_MIDDLEWARE_ALIAS = 'Cache'
CACHE_MIDDLEWARE_SECONDS = 60
CACHE_MIDDLEWARE_KEY_PREFIX = ''
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
Cache service is running after I typed: $ memcached -p 11211 &
But when I try to run server, the following error shows up:
django.core.cache.backends.base.InvalidCacheBackendError: Could not find config for 'Cache' in settings.CACHES
What I am doing wrong?
The CACHE_MIDDLEWARE_ALIAS setting tells the django middleware which cache to use: https://docs.djangoproject.com/en/3.1/ref/settings/#cache-middleware-alias
Here it is set to 'Cache' but there is no cache of that name in your CACHES setting. You probably don't want a different alias and should just use the default of 'default' so just remove that setting.
I've built a simple SPA CRUD web app with Django, Vue and Docker(-compose).
Since I've finished developing the app, I'm now preparing for the production environment, that is, using bundle.js and bundle.css files.
When I try to load the main page, http://localhost:8000, no CSS or JS
are being loaded because I'm getting this error in the browser's console:
GET http://0.0.0.0:8080/bundle.css net::ERR_CONNECTION_REFUSED
GET http://0.0.0.0:8080/bundle.js net::ERR_CONNECTION_REFUSED
I really don't know why it is giving that error or how to fix it.
This is my vue.config.js file:
const webpack = require("webpack");
const BundleTracker = require("webpack-bundle-tracker");
module.exports = {
publicPath: "http://0.0.0.0:8080/",
outputDir: "./dist/",
filenameHashing: false,
configureWebpack: {
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
]
},
chainWebpack: config => {
config
.plugin("BundleTracker")
.use(BundleTracker, [{ filename: "./webpack-stats.json" }]);
config.output.filename("bundle.js");
config.optimization.splitChunks(false);
config.optimization.delete("splitChunks");
config.resolve.alias.set("__STATIC__", "static");
config.devServer
.hotOnly(true)
.watchOptions({ poll: 1000 })
.https(false)
.disableHostCheck(true)
.headers({ "Access-Control-Allow-Origin": ["*"] });
},
// uncomment before executing 'npm run build'
css: {
extract: {
filename: "bundle.css",
chunkFilename: "bundle.css"
}
}
};
This is part of my settings.py file:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "assets"),
os.path.join(BASE_DIR, "frontend/dist"),
]
# STATIC_ROOT = "" # The absolute path to the dir for collectstatic
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'dist/',
'STATS_FILE': os.path.join(BASE_DIR, 'frontend', 'webpack-stats.json'),
}
}
When I run the npm run build command I get notified that both the bundle.css and the bundle.js files have been generated:
File Size Gzipped
dist/bundle.js 150.73 KiB 51.05 KiB
dist/bundle.css 192.06 KiB 26.89 KiB
Images and other types of assets omitted.
DONE Build complete. The dist directory is ready to be deployed.
INFO Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html
I really don't know why it is giving that ERR_CONNECTION_REFUSEDerror or how to fix it.
For production remove publicPath from your vue config file, I suppose you used that for development purposes, but in production you should only create the bundle and serve it to the user.
What's probably happening is that in your Docker setup you don't have the webpack dev server running (and you don't need it), thus you hit a connection refused error.
I've been reading a million answers in this subject but I can't find a solution that works.
I have a Django DRF backend that is used for all the API calls and a separate Angular(5) front-end project. The Django backend is deployed to Elastic Beanstalk and is working properly.
All I want to do is to deploy the angular frontend without needing to add template tags to the htmls. Better yet I don't want to change anything in the built dist Angular folder.
I see there are several approaches - As far as I can understand the best one would be to serve the index.html through a regular Django url and then expose all the dist folder (the static files).
I achieved the first part using:
urlpatterns += [
url(r'^$', TemplateView.as_view(template_name='index.html')),
url(r'^(?P<path>.*)/$', TemplateView.as_view(template_name='index.html')),
]
and defining the dist folder as a template folder in settings.py:
ANGULAR_APP_DIR = os.path.join(BASE_DIR, 'client/dist')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [ANGULAR_APP_DIR],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.i18n',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Currently, I define an s3 bucket to hold all static files. 'collectstatic' runs when I deploy the code and puts all the static files in the right location on s3, including the files in dist, because I added this:
STATICFILES_DIRS = [
ANGULAR_APP_DIR,
]
I can't figure out how to serve all the other static files (js, css assets) from the base url ("/") which is the location that the Angular app is expecting it to be in.
I'm not sure if I should try redirecting the calls Through Django urls to the s3 bucket, or if I should do it totally separate from Django with routes on s3.
What would be a better approach and how is it done?
Thank you!
What LOCATION should I point Memcached to after deployment on pythonanywhere server? For local I am using this setting and things are working fine.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
I need to change 'LOCATION' to replace localhost. Any guidance?
You can location set to a path.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'unix:~/memcached.sock', }}
However, I don't think pythonanywhere lets you use memcache, since you can't use 'sudo apt-get' on a pythonanywhere console, and using memcache requires you to install it. (sudo apt-get install memcached)