Heroku doesnt migrate models on Django - django

I just deploy my Django app to Heroku but I cant migrate my migrations to heroku. First I run :
heroku run python manage.py migrate all the migrations list as OK but when I showmigrations, none of them is migrating (all blank [ ]).
Then I try heroku run bash and migrate from there, everything seems ok even showmigrations from bash showing all of the migrations is working. I even manage to create a superuser. But when I open my admin page and log in with superuser it shows 'account.account' table does not exist and when I check showmigrations again all of the migrations are gone. I have been repeating this migration over and over and still can't figure this out.
Does anyone know what I am doing wrong here?
Edit :
I don't know if this is related but when I push my project to Heroku the first time, I'm using Postgre with this setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': 'localhost',
'PORT': '5432',
}
}
but when I try to run migrations it shows an error something like cant access localhost with 5432 port, it turns out that Heroku trying to access postgre in my localhost instead of using Heroku Postgres. The solution I found is to dump my db and reload it to Heroku. And since I don't know how to set that up. I just comment that postgre setting and replace it with default django setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
create my dbsqlite file, running migrations again then once again push it to Heroku, hoping to find an easy way out but instead ended up in this problem

I'd recommend you to push a migrated project to heroku that is
python manage.py migrate
then push that migrated project to heroku
the reason your superuser doesn't get stored while creating superuser from heroku bash is because heroku has an ephemeral drive i.e heroku clears all the modified data from orignal push after equal interval of time.

It turns out that I forgot to install django-heroku. More on this : https://devcenter.heroku.com/articles/django-app-configuration

Related

django-admin dbshell raises django.core.exceptions.ImproperlyConfigured

I know this question has been asked before, but none of them worked for me so far, so I'm going to give it a chance here.
I'm trying to use MySQL as my database in django, but when I modify the settings.py and run the command:
django-admin dbshell
I get the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,
but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure()
before accessing settings.
What I did:
- I'm running windows 10.
using pipenv, I create fresh virtual environment.
install django.
start new project.
edit the settings.py
in the settings.py I change the DATABASES to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_db',
'USER': 'root',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
also pip installed mysqlclient
Weird thing is that when I make migrations, new tables are created, which means that DB works. why doesn't the command
django-admin dbshell
work?
based on django docs, manage.py does same thing as django-admin, and also adds settings to the sys path, therefore manage.py should be used instead of django-admin
https://docs.djangoproject.com/en/2.1/ref/django-admin/

Error on deploying Django app to Heroku

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!

setting database: postgresql in django 1.8 in heroku

I have been struggling for this issue for the whole days while no solutions at all. so I post it here.
I am trying to set up a blog website in Heroku via Django 1.8 which uses Python 3.4.3. I follows the instructions from Heroku website here.
I use "foreman start" to run Django project in my Mac and I already installed all dependence.
Part of my setting.py file involving the database initially looks like:
import dj_database_url
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
Then I got error: ImproperlyConfigured at /settings.DATABASES is improperly configured. Please supply the ENGINE value.
Then I modify the files by adding one line supplying the ENGINE value:
import dj_database_url
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
Based on this post answered by Or Arbel, it should work. But I got another error: ImproperlyConfigured at /settings.DATABASES is improperly configured. Please supply the NAME value.
What should I do next? Actually my Django project is very simple and does not involve any database operations(may need in the future). I just want to make it works on Heroku. Thanks!
Do I need to create a database to continue? I just want to make the webpage works.
Thanks for your guys help, specially souldeux.
Update:
I have fixed the issue by using souldeux's method by providing more informations about the database. Here I want to emphasis that it seems the code from the original Heroku tutorial does not work for Django 1.8:
import dj_database_url ####not working for my case
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
Initially I did not create a database because I think it is not necessary for simple projects, based on my understanding obtained from Heroku tutorial. Actually it does need to create a database in Heroku to make it works. The tutorial is here. You need run "heroku config -s | grep HEROKU_POSTGRESQL" to get the database information. The format is like:
scheme://username:password#host:port/database
So you can get 'database', 'username', 'password', etc.
Afterwards, modify the 'settings.py' according to souldeux, then run following codes:
git add .
git commit -m "Ready to go to Heroku"
git push heroku master
heroku run python manage.py syncdb
Now it works. But other issues arise like my webpages do not show images... Anyway it solved. Please confirm my solutions, thanks.
I think you need to add more information to your database definition. For instance, here's what my own database entry looks like in my settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django',
'USER': 'redacted',
'PASSWORD': 'redacted',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
If you don't have a database user and password to enter in the fields marked redacted then you need to make sure you have the actual database created and psycopg2 installed.

Mezzanine: where is the database created by `manage.py createdb`

I have the following database settings in Mezzanine:
DATABASES = {
"default": {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lucidDB',
'USER': 'lucid',
'PASSWORD': 'xxxxxxxx',
'HOST': 'localhost',
'PORT': '',
}
}
I run the command python manage.py createdb and then answer yes to the question Would you like to fake initial migrations? (yes/no):.
Note: south was installed.
My questions:
I checked in postgres (postgres# psql --> postgres=# \l), but didn't find the database lucidDB. However, the system runs OK. Where exactly is the database created?
What does it mean by fake migration?
I didn't run syncdb or makemigrations --> migrate yet, why the system worked?
You didn't find the LucidDB in postgres because it wasn't created, you need to create it with the postgres shell see here for detailed description.
Faking a migration is marking it as complete without actually changing the database schema, it will simply add an entry in the migrationhistory database, see detailed description here.
On the Mezzanine docs it is stated that createdb is a shortcut for syncdb and migrate commands, see here for detailed description.
Hope this helps!

Django to work with south requires MySQLdb

I am following the django instruction to learn django in eclipse.
I came to the part of running cmd
python manage.py migrate
and it complains about unknown command migrate.
Googled. Knew that it requires South module to be included. I downloaded/installed south, and added 'south' in the INSTALLED_APPS.
I ran the command again, this time it complains
import MySQLdb as Database
ImportError: No module named 'MySQLdb'
So I looked for MySQLdb, only to find that there is none for python 3.
I could not find anything useful. So what do you do to make django to work with mysql?
I know there're other connectors around, but I am trying to follow the django tutorial and it seems that 'migrate' cmd must use 'south' and 'south' must use MySQLdb(?)
--- update ---
Here is the DB settings in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '******',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
I suspect 'ENGINE' has to be something else, but I failed to find enough information online to figure it out...
You can switch to any database you want MySQL or postgresql or sqlite etc for your django app. South uses the default database engine from your django setting DATABASES. As stated here
South automatically exposes the correct set of database API operations
as south.db.db; it detects which database backend you’re using from
your Django settings file.