Django on Heroku - settings.py for development vs. production - django

I deployed a django application to Heroku using this tut.
And I know I can use heroku local to run the app locally.
But how do I migrate locally? as in create a database locally, not in the cloud, rather than push changes to the cloud and see if it works?

You should be able to override the DATABASES setting for your local environment to point to a different database:
# settings.py
. . .
# at the end
try:
from local_settings import *
except ImportError:
pass
# local_settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'whatever',
'USER': 'whatever',
'PASSWORD': 'your-password',
'HOST': '127.0.0.1',
'PORT': '5432'
}
}

Related

Heroku Posgres' DATABASE_URL cannot be loaded in Django's setting.py and application error occurs

When building an API server using Django+REST framework and uploading it to Heroku, hard-coding Heroku Postgres' DATABASE_URL in the DATABASES of setting.py as follows will work on Heroku without problems. (xxx is originally the information of DATABASE_URL, but we dare to hide it this time)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'xxx',
'HOST': 'xxx',
'PORT': '5432',
'USER': 'xxx',
'PASSWORD': 'xxx,
}
}
However, you can add a .env file to your Django project, stating the following (where xxx is the information originally entered for the DATABASE_URL, but we've dared to hide it this time)
DATABASE_URL=postgres://xxxx
In setting.py
import dj_database_url
from dotenv import (
find_dotenv,
load_dotenv,
)
load_dotenv(find_dotenv())
DATABASES = {
'default': dj_database_url.config(conn_max_age=600),
}
If I put something like this, it works in the local environment, but when I upload it to heroku, I get an application error.
Do you know the cause?

Sync all tables from sqlite to postgres

I have a django application and deployed it on DigitalOcean. But the only problem is, new models, admin models, tables are not showing in django admin dashboard which is on running on server. Although I pushed al changes to github, pulled them from, and made migrations, again nothing changes. How can migrate all tables from db.sqlite3 to postgresql ?
I would bet your settings.py database is still configured to the default sqlite.
# default settings.py using SQLlite
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Change that to your actual postgresql database address
Since you're deploying, it is best practice to configure those parameters as separate environnment variables, as such in your settings.py. Environnment variables would then have the actual values.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ['POSTGRE_NAME'],
'USER': os.environ['POSTGRE_USER'],
'PASSWORD': os.environ['POSTGRE_PASSWORD'],
'HOST': os.environ['POSTGRE_HOST'],
'PORT': os.environ['POSTGRE_PORT'],
}
}

Is the following enough to protect my secret key and database password for heroku deployment?

I have added the environment variables through to get secret key database name and password. Now anything else to make it more secure.Let me know.
Used "Advance system settings" and under that added environment variables in "User variables" .
Also this is going to be my first deployment on 'heroku' that's why following a tutorial.
SECRET_KEY = os.environ['DB_SECRET']
databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'portfoliodb',
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
'HOST': 'localhost',
'PORT': '5432',
}
}
Heroku has a package that handles that for you. More info here. Essentially, it overrides default with secure credentials automatically generated in the Heroku platform.
Add the following import statement to the top of settings.py:
import django_heroku
Then add the following to the bottom of settings.py:
Activate Django-Heroku.
django_heroku.settings(locals())

Changing from Sqllite to Postgres on Heroku- why it is not working?

I am a bit confused , hope someone can help me out
Originally I uploaded my django app to heroku account with sqllite db
This is what I had in my settings file for DB
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
} }
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
It didnt work great since SQLlite was getting flashed to its original state every 24 hours (but worked perfectly since I needed demo system)
However now I have to make it productive so I want to change the db to connect to postgres . So I used credentials from postgres DB I created with heroku and my db in settings looks like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'd4drq1yytest',
'USER': 'xvvqgkjtest',
'PASSWORD': 'test5y55y5y5y5y5y5y5y5y5y54y45',
'HOST': '777-77-77-67-7.compute-1.amazonaws.com',
'PORT': '5432',
}
}
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
But even I changed the settings file and redeployed the new file I still see that my heroku app is connected to my sqllite DB.
What did I do wrong? (I deploy using a master brunch from my github.)
If you have not already, you might need to add heroku postgres as an app in your heroku app's dashboard.
It seems like you have used the same code that I used to connect my heroku's postgres to heroku. You should only have to state
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
then make sure you have pip installed whitenoise and added that to requirements.txt. After commmiting, and pushing to heroku, you may want to run heroku run bash on cmd or terminal. Then you may want to run python manage.py makemigrations yourappname afterwards, you may want to run python manage.py migrate, and if you get no errors, you might want to check on your heroku dashboard for your app, clicking on postgres, to see if any of the postgres' tables are in use. If they are, then you are using the heroku postgres.
you may not need the lines of code below for heroku
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'd4drq1yytest',
'USER': 'xvvqgkjtest',
'PASSWORD': 'test5y55y5y5y5y5y5y5y5y5y54y45',
'HOST': '777-77-77-67-7.compute-1.amazonaws.com',
'PORT': '5432',
}
}
this should be where you are calling the database you will be testing with your localhost.

Django 1.8 migrating local postgres database into a heroku database

I've been stuck on this part for a while.After completing all the other steps when I enter git push heroku master, it pushes all the files but how do I migrate my local database? There are no commands for that. My settings.py file is still pointing to the local postgres database.How do I migrate this data into the heroku database?
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'music',
'USER': '**',
'PASSWORD': '**',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Did you followed the heroku django tutorial. From the django-settings they are clearly mention how heroku will detect database url using dj-database-url package.
So just define this setting below your settings.py file then you are good to go.
For Importing and Exporting to heroku postgres please see this heroku-postgres-import-export