When deploying updated Django code on server, the database resets - django

I have built an app which uses Django as a Rest API (Django rest framework). After having deployed the code on digital ocean using production grade services, everything works well. The postgresql database populates itself correctly . There is no reset of the server like on Heroku.
Except, if I change a file and push the change through gitlab(Digital ocean picks up the change and automatically deploys the updated version of the server), the database resets to its original state. I would like the server to stay as.
I have removed all migration files before pushing updates, but to no avail. I imagine it has to be something rather trivial but I can't find it.
The database is hosted also by digital ocean. I havent done any configuration. Wouldn't it be managed automatically as DO deploys the code on its servers?

I figured out what went wrong. Using the sqlite db while developping is one thing, but assuming when deploying your django app on a service provider that it will handle the whole db management is a 1000 steps too far.
Logically, whenever I pushed a new update to the server, the db would reset using the sqlite local db file of the project.
So I created a specific db on digital ocean, change the database config on the settings file of my django project to connect to it. Making the project use the external db. then I Just had to migrate project db structure to the external db.
And Voila. problem resolved

Related

how to do makemigrations/migrate in gnadi(python/postgresql web hebergement)

I am a python developer and new to GANDI (python/PostgreSQL web hosting). There is no document to migrate databases to their server. they have their document to deploy a Project Django but not very clear there are a lot of things missing. After taking a lot of time on the internet, I managed to deploy a Django project in their server. The site works smoothly with an SQLite database. Now I would like to make a Django Project with PostgreSQL. I have an emergency console, but I would like to work locally and then I would like to migrate the DBs to their phpPGadmin server. I just need your help to do makemigrations/migrate to thier PostgreSQL(PhpPGadmin) server. Thanks in advance.

How do I prevent my Django Database from being overwritten

Currently I have a Django application running on my personal website from PythonAnywhere.com. When I make updates to the site I login to PythonAnywhere and git pull to update my application. When I do this the data that was entered through the website since the last update gets lost from the database when I update. What can I do to overcome this?
I am currently using the SqlLite version that comes preinstalled with the Django App. This likely could be part of the issue but need some help understanding how to overcome it.
I presume that your Django project is connecting to the SQLite database that gets generated when you create a new project and you are pushing that database in your deployment. That database won't be suitable for production and won't retain your changes, you instead need to setup a database that your application can connect to.
Here is some information on PythonAnywhere about databases:
https://help.pythonanywhere.com/pages/KindsOfDatabases/
And the Django documentation:
https://docs.djangoproject.com/en/4.0/ref/databases/

What is the best Workflow for Updating a Web App

I'm pretty new at this but I recently deployed my first web application using Django, Postgres, Nginx, and Gunicorn on Digital Ocean.
I went to make some updates and ran sudo service gunicorn restart to see the changes. I wiped my entire Database. I obviously need to work on setting up a better workflow for deploying to production.
Does anyone have any suggestions? What are some best practises when re-deploying a Django App with a Postgres DB? I've read some articles about backing up my DB (which I'm definitely going to be doing in future), but what else can be done to ensure a seem-less transition?

How to have a site hosted on Heroku with database being stored locally on my computer?

I am hosting a Django site on the Heroku. However, Heroku does not allow databases with over 10 000 records stored for free. Can I store my database locally on a computer I have direct access to and still host the site on Heroku?
It is possible to use external database in Heroku by adding configuration:
heroku config:add DATABASE_URL=mysql://user:pass#server:port/database_name
But the question is what to I write for server in this case?
This is possible, but you will have to open up some ports on your local computer. Please look into port fowarding if you are into this. You are even able to post your whole website on your computer.
Personally I wouldn't recommend doing this, because you computer could become more vulnerable and you have to rely on your own home network and computer speed. Scaling is not easy with this setup.
However, there is (some kind of) an alternative. Django uses SQLite as the default database. You are able to use SQLite on Heroku. Please note that SQLite is not build for websites that interact a lot with your database and you cannot push a new build as easily as you would normally do. You will have to export the database first and then rebuild it later as Heroku creates a fresh website each time it builds (and the new database entries will be gone with that).
In other words: I would recommend to just pay for an upgrade of the database or move to another hosting company. I am sorry.

Set up an existing Django app on another server

This must, must, must be a duplicate but I can't find the answer either here or on Google.
I have an existing Django app and I want to deploy it on another server.
What are the steps I need to go through to get it running elsewhere, in words of one syllable?
At a minimum, presumably:
create a project on the new server
copy over all my app's files into that project
edit localsettings.py or equivalent with local database settings etc
run python manage.py syncdb to create my database
load fixtures
run tests
Is that it? Are there any unofficial or official instructions for copying an app elsewhere?
Thanks.
The most basic, easy way to do it is:
Set up the server environment so that it matches the current environment as closely as possible. The most important thing is that you're using the same version of Django (obviously). You can run a different database, but it makes porting from the old server to the new server a little more involved. Otherwise, you will just have to adjust your settings to match the new server, if they can't be duplicated (ie, user names, etc).
Copy over your code
Dump your old database structure and data into the new database
Running startproject isn't strictly necessary. Your web server just needs to be configured correctly, to run django with the appropriate settings.py file -- and Django takes it from there.