I am trying to integrate filebased session in django. I have followed all the provided details But session is not working.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
}
}
I am getting below error
Exception Type: OperationalError
Exception Value:
no such table: django_session
Looks like you confused caching and session frameworks. Your current settings is affecting only cache system. To use file based session you need to add SESSION_ENGINE and SESSION_FILE_PATH settings:
SESSION_ENGINE = "django.contrib.sessions.backends.file"
SESSION_FILE_PATH = "/var/tmp/django_cache"
Related
I devoted today to migrating my app from my local environment to Heroku. It's been frustrating and fun at the same time, but now I'm very stuck.
I have a schema called DCPViews which I want the DB connections to default to when running queries. I've read all the relevant tutorials / posts / tips and here's where I am:
DB hierarchy
postgres (default system database)
-> DCP (app database)
-> DCP (base tables schema)
-> DCPViews (views layer schema)
settings.py
import django_heroku
import dj_database_url
...
DATABASES = {}
# DATABASE_URL = 'postgres://<user>:<pass>#<host>:<port>/<db_name>?currentSchema=<schema>'
DATABASE_URL = 'postgres://' + \
config('DB_USER') + ':' + \
config('DB_PASSWORD') + '#' + \
config('DB_HOST') + ':' + \
config('DB_PORT') + '/' + \
config('DB_NAME') + \
'?currentSchema=' + config('DB_SCHEMA_NAME')
DATABASES['default'] = dj_database_url.config(default=DATABASE_URL, ssl_require=True)
...
# Configure Django App for Heroku
django_heroku.settings(locals())
The problem
I've tried everything to get Heroku to use the correct DATABASE_URL (with currentSchema = DCPViews), but no luck. I don't have DB permissions to create new roles nor set a search path for Heroku's default DB user. It also won't let me export DATABASE_URL manually and doesn't seem to accept the value I pass in the settings.py file. The heroku config -s command always returns the same DATABASE_URL value.
Everything works fine in my local environment, but this is a major snag. How can I get Heroku to use the correct search_path or default to the DCPViews schema when running queries?
Update
I'm send send the DB credentials and currentSchema I want to use in my settings.py file, but Heroku seems to ignore them and overwrites them when it creates the DATABASES['default'] key. Here's the Django debug when I load a page in my browser:
DATABASES
{'default': {'ATOMIC_REQUESTS': False,
'AUTOCOMMIT': True,
'CONN_MAX_AGE': 600,
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': '<host_name>',
'NAME': '<db_name>',
'OPTIONS': {'sslmode': 'require'},
'PASSWORD': '********************',
'PORT': 5432,
'TEST': {'CHARSET': None,
'COLLATION': None,
'MIRROR': None,
'NAME': None},
'TIME_ZONE': None,
'USER': '<user_name>'}}
DATABASE_URL
'postgres://<user>:<pass>#<host>:<port>/<db_name>?currentSchema=<schema>'
I cannot comment yet, but since your settings file is using an environment file, you can just go to your heroku app in heroku.com and go to your database that is connected to your app. Then go to settings and click reveal config vars. Change your DATABASE_URL to whatever your need it to be (mind you it wil probably be just an extension off of your actual heroku postgres database url)
Well, I was on the right path. I added the search_path to the ROLE and that did the trick:
ALTER ROLE <role_name> SET search_path = <go, pirates>
Hello i built my first Django app and i tried to make it live using heroku.
My app works fine locally. When i deployed it on heroku i get this error: "Application error
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details."
I am really new to this. I dont know where to look for logs.
I tried this tutorial to deploy the app.
My code on github.
Any ideas?
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
I had a look at your code on Github. These are the mistakes you have made
You should rename Procfile.txt to Procfile
You also have to change your DATABASES settings in your settings.py. You can have a look at this answer.
Hope that helps!
So Ive managed to screw something up between my production database settings for heroku.
Running the production settings locally I receive the error,
ImproperlyConfigured at /
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
And I get the following error when deployed to Heroku seen here: http://tulsa-staging.herokuapp.com
Exception Value:
cannot concatenate 'str' and 'NoneType' objects
It appears that it has to do with my Database settings but I'm just not sure how to resolve this.
prod.py database settings for Heroku
urlparse.uses_netloc.append('postgres')
urlparse.uses_netloc.append('mysql')
try:
if 'DATABASE_URL' in os.environ:
url = urlparse.urlparse(os.environ['DATABASE_URL'])
DATABASES = {
'default':{
'ENGINE':'django.db.backends.postgresql_psycopg2',
'NAME': url.path[1:],
'USER': url.username,
'PASSWORD': url.password,
'HOST': url.hostname,
'PORT': url.port
}
}
except Exception:
print 'Unexpected error:', sys.exc_info()
Any thoughts on how to resolve this?
Why aren't you using dj-database-url?
It makes this so much easier:
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
It automatically looks for and parses the value in env['DATABASE_URL'], falling back to what you pass into default.
It's what Heroku actually recommends you use.
I'm trying to get my local dev django app to work after following these instructions on adding env database settings.
https://devcenter.heroku.com/articles/django-injection
I followed the instructions but get the following error when my app tries to access the local database
Request Method: GET
Request URL: http://localhost:8000
Django Version: 1.4
Exception Type: ImproperlyConfigured
Exception Value:
You need to specify NAME in your Django settings file.
My database settings originally,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db', # Or path to database file if using sqlite3.
'USER': 'foo', # Not used with sqlite3.
'PASSWORD': 'bar', # Not used with sqlite3.
'HOST': 'localhost',
'PORT': '5432',
}
}
the heroku article says to add the following to the settings file
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
how do I get dj_database_url.config to use my my dev settings when the DATABASE_URL is not available in dev?
You can just add your dev settings to the default values like this...
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://foo:bar#localhost:5432/db')}
Use this in your settings.py:
DATABASES = {'default': dj_database_url.config(default=os.environ['DATABASE_URL'])}
and in your .env file have this:
DATABASE_URL=postgres://localhost/yourdbname
when you launch with "foreman start" it will look at the .env file and create all those environment variables, just like running on Heroku itself. Type "heroku config" to confirm that you have a DATABASE_URL set, which you should if you added the postgres database addon.
Just set an environment variable on your operating system and check weither or not it's set. For instance, with a UNIX system:
# In ~/.bash_profile
export LOCAL_DEV=true
# In settings.py
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
if bool(os.environ.get('LOCAL_DEV', False)):
# Override DATABASES['default'] with your local database configuration
Also, if you need to set an environment variable on your heroku space:
heroku config:add MY_VAR='my_value'
I just tried this and here is my code:
import dj_database_url
local_db = 'postgres://django_login:123456#localhost/django_db'
DATABASES = {'default': dj_database_url.config(default=local_db)}
My database name is "django_db", user name is "django_login", password is "123456".
My code can run both in local machine and heroku.
import dj_database_url
DATABASES = {'default':
dj_database_url.config(default='postgres://yourusername:yourpassword#yourhosturl:5432/yourdbname')}
** Replace bold string with your database settings
if you are using local database then replace yourhosturl by localhost
So I want to cache some data in mysql and some in memcached.
at the moment I have this In my config file, but i don't know how to write router for cache back end.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
I use multi databases structure and I know how to write multi database routers.
in settings.py
DATABASE_ROUTERS = ['oceankeys.dbrouter.SphinxRouter','oceankeys.dbrouter.DefaultDbRouter']
Any one know how to make Django caching BACKEND router?
Thanks
I don't believe the Django cache framework can mimic db routing in general.
For the site cache using the cache framework middleware you have to specify the name of the cache in settings.py, e.g.:
CACHE_MIDDLEWARE_ALIAS = "my_cache_alias"
For a page cache you can manually specify the name of the cache in the decorator, e.g.:
#cache_page(60 * 15, cache="my_cache_alias")
def my_view(request):
...
I'm not sure cache routing really makes sense for site and page caching so I don't have a problem with the way this is designed.
Now, for your case where you are using MySQL as a database cache backend you can set it up and make a router as per the Django docs section on database caching. For example, this would be your CACHES setting:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
'my_cache_alias': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache_table',
}
}
Then create a router that identifies which cache backend to use for which models. It looks and works exactly like DB router (as you should be able to see from the doc section on database caching and multiple databases) with the exception that it returns a cache alias instead of db alias.
e.g
settings.py
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
},
'myalias':{
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache_table',
}
}
views.py
from django.core.cache import caches
cache = caches['myalias']
cache.set('my_key', 'hello, world!', 30)
print cache.get('my_key')
You can see the detail in Django’s cache framework (section:Accessing the cache)