Heroku-deployed Django webpage doesn't update when DB changes - python

I have deployed an app in Heroku using Django. The Django program uses a SQLite database db.sqlite3 on root directory to populate its page. Separately, there is also a Node.js scraper program that inserts to that database.
The problem is that the hard-refreshed webpage shows the same data even after the content of the database changed. Curiously, this does not happen when it is tested locally with python manage.py runserver. How can I fix this problem?
Thank you in advance!
For reference, here is my requirements.txt file:
Django==1.10.6
gunicorn==19.7.1
Pillow==4.0.0
selenium==3.3.1
whitenoise==3.3.0

You cannot use sqlite on Heroku.
An sqlite db is stored as a file on the local filesystem. But in Heroku the filesystem is ephemeral and is not shared between dynos. Every time you redeploy your app, or scale your process, or in your case launch a worker, you get a new filesystem with a different copy of the db file.
Use the proper Postgres support via the add-ons.

Related

Deploy Django app to Heroku AFTER DATABASE?

I have deployed my django website to Heroku but since the website fields are dependent upon a database that is on my local machine. I've tried using Postgres but the database on Heroku doesn't populate with the data I need it to before the app runs. Has anyone experienced this? Do I need to use an exteranl database on AWS or something (in which case, what is the best way to do this?)
Use load data and dump data: https://docs.djangoproject.com/en/3.2/ref/django-admin/#loaddata
First dump the data on your local machine
python manage.py dumpdata ..other_options > data.json
add it to git and push to herkou
git add data.json
git commit -m "Added data"
git push heroku master
now on Heroku can use loaddata to load your data to database
heroku run python manage.py loaddata data.json
And your done.
I have deployed a Django project on Heroku where I had data in models even after the deployment the data remained in the models. I worked on SQLite in production. Heroku works with Postgres but on deploying also my data remained safe. I just used git push heroku main

Pulling data from heroku for django website

I made a website on django and hosted it on heroku. It uses the default sqlite3 database.
When adding some entry on the website, the heroku db is updated. I am unable to pull that entry on my local db.
When I push any changes to the heroku, all those entries that were added from the website are deleted.
How can I pull those entries from heroku?
When you push your changes to Heroku, you are also pushing your SQLite3 database file, which overwrites the changes that were done to it on the Heroku server.
If you want the database to stay the same, dont add it to your commits when you push to Heroku.
As Daniel said, you should just switch to a free Heroku PostgreSQL database instead of using SQLite3. This way you can download the Heroku database onto your local system via backup/restore and any changes you push to Heroku won't overwrite the existing data in the database.
You can find instructions on how to implement Heroku PostgreSQL into your django application here: https://devcenter.heroku.com/articles/heroku-postgresql#connecting-with-django

How to update my production database in a django/heroku project with a script

I want to update a field in my users table on my django project hosted on heroku.
Is there a way I can run a script(if so from where?) using what?
That allows me to update a field in the database? I could do this manually in the django admin but it would take way to long as there are large number of users.
Any advice is appreciated.
i suggest you update the data in your local then make a fixture, commit and push it in your heroku. then do load the data using the terminal
update data (locally)
make a fixture (manage.py dumpdata)
commit and push to heroku
login via terminal (heroku login)
load the data (heroku run python manage.py loaddata .json)

Download sqlite database from Heroku

I have a worker running python script every 2 hour on Heroku.
The problem is each time I 'pull' the changes from git.
There is no changes at all for the sqlite3 database.
But I am sure the program is running and the database has changed by looking at the log file.
heroku log
How to retrieve the .db file then ?
It sounds like you have a little misconception. Heroku's git support is effectively one-way; you can use it to push new code to be run on the server, but you can't use it to copy files from Heroku back to your local tree.
Unfortunately it looks like there's not a good easy way to copy a file from your app to your local machine; you can use heroku run console to get a bash shell, and then scp a file out, but you're "pushing" it out of Heroku, and thus run can only copy to things with valid IP addresses.
If you're really using sqlite for your app's storage, though, you're going to run into a bigger problem. The filesystem for your app on Heroku is ephemeral, in that changes you make can be wiped out at any time. Heroku will delete your app's local storage and start over fresh whenever it wants to.
The right way to do it is use Heroku's built-in Postgres support and store your application's data there. Not only will it persist, but you'll be able to access it directly using the Postgres command-line tools.
Accessing the heroku console can now be done with:
heroku run bash
then i downloaded the linux gdrive application and ran in locally in the folder to upload my file to google drive. https://olivermarshall.net/how-to-upload-a-file-to-google-drive-from-the-command-line/ (skip step 4 and run with ./ like this ./gdrive upload my_file.txt
the other suggestion of heroku run console did not work for me (running a python flask app)

sqlite3 -- can't open database file

For background, I'm trying to migrate my sqlite3 local db to a postgresql db on Heroku.
I have no problem accessing the db locally through my (Django) development server. However, when I try to push the db to Heroku, it says it can't open the file.
heroku db:push sqlite://path/to/db --confirm my-app-name
I get the following message:
Loaded Taps v0.3.23
Warning: Data in the app 'my-app-name' will be overwritten and will not be recoverable.
Failed to connect to database:
Sequel::DatabaseConnectionError -> SQLite3::CantOpenException: could not open database: unable to open database file
Most everything I've seen on Google relates to some bugs in earlier versions of Tap. Otherwise, I'm not sure what I should do here.
I've tried following the advice of this question and others about permissions, but I have full read and write access to the file and containing folder. I'm not too experienced with permissions -- do I need to switch ownership of the db to another user?
If /path/to/database is an absolute path, you need to do:
heroku db:push sqlite:///path/to/db --confirm my-app-name
Note the third slash. It could also be a permissions issue, in which case you want to either change the owner to the current user, or give at least read permission to the database (644).
I would try to save the data from sqlite3 to a fixture via dumpdata and then, after switching to postgres, load it again via loaddata.

Categories