Connecting to Django shell on Azure Websites - django

On Heroku, I can connect to a Django shell by running
heroku run python manage.py shell
Is there an equivalent on Azure Websites?

Aha! I realized I can just connect to the database by adding it as an additional database on my local version of the app.
Not exactly what I was thinking of originally, but it seems to solve all of my use cases, for example:
python manage.py syncdb --database=live

Related

How to migrate a Django app with postgres database

I have just inherrited a jDango app with .tar.gz compression extention and a .sql file which is a postgreSQL database dump. My mission which I did not choose to accept is to somehow find its way onto AWS. I'm not familier with Django. I believe the app was built with the Ubuntu OS.
I thought that maybe the best thing would be to get postgreSQL and Django working on my local machine. I have all working in vanilla on Windows, and Ubuntu (virtual box) and have set up Amazon Lightsail with a postgreSQL database..
I'm a bit stuck as to what to do next to migrate the app. Would it all be CLI/Cmd Prompt/Terminal or is there a procedure I should be using.
I'm guessing it's not as simple as importing the database and then copying files over and replacing existing vanilla Django ones? Any pointers would be of great help. Thanks.
Open your editor terminal and write code below:
python manage.py makemigrations
python manage.py migrate

How do I recommit database to Heroku after destroying DB in the Heroku dashboard

I've built an app using Django etc. I ran into a whole bunch of problems with database on Heroku after accidentally pushing to production before running heroku run python manage.py makemigrations and heroku run python manage.py migrate.
I decided to just completely destroy the database currently on Heroku and want to recommit my app's models to heroku. My hope is to completely start from a clean slate.
I just want to make sure I do this correctly so I'm asking for clear instructions about how to recommit my app's database to Heroku after destroying the database in the dashboard.
Current error I get when I open my app online is:
settings.DATABASES is improperly configured. Please supply the NAME or OPTIONS['service'] value.
Thanks!
If you completely removed your database you'll have to provision another one. Assuming you are using Heroku Postgres¹, you can do that using the Heroku CLI like so:
heroku addons:create heroku-postgresql:hobby-dev
Here we are asking Heroku to provision the heroku-postgresql add-on using the free hobby-dev tier. Modify as needed.
You can also provision add-ons using the Dashboard if you prefer.
As part of the provisioning process, Heroku Postgres will automatically set a new DATABASE_URL for your app. As long as your project was using this variable before you deleted your old database, it should find the new database without requiring any changes.
¹If you were using a different database add-on the solution should be more or less the same. Provision a new instance of the add-on, let it set its environment variable, and if your app was using that variable before it should pick it up on its own.

Is there a way to create superuser for Django apps on Heroku without using the CLI?

I changed my app to use PostgreSQL on Heroku instead of the SQLite database that I have been using in development. Now I want to log into my admin portal on my hosted app, but I am not sure how to create a superuser.
Is there a way to push my local database to my Heroku database? I have been deploying through GitHub (not using the Heroku CLI) so I was wondering if there was a way without using the CLI.
If there isn't a way without using the Heroku CLI, could someone tell me how I would init the Heroku git repository, considering the fact that I already have a GitHub repository that I have been using?
This is my settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)
A couple ways you can do this:
Include a DataMigration in the migrations file, this can be done with ./manage.py makemigrations <app_name> --empty. In that file you can write a datamigration that adds a superuser.
You can create a custom start script for the app, in that script, invoke a ManagementCommand that ensures a superuser is always there.
Is there a way to push my local database to my Heroku database?
Yes, if you are using the same database software locally are you are on Heroku. For example you can push a database into Heroku Postgres using the Heroku CLI. But if you are still developing with SQLite (which is not recommended as these products aren't drop-in replacements for each other) there is no simple way to push your database.
I know you are asking for a solution without using the Heroku CLI, but I'm still going to recommend using it.
Use of the Heroku CLI and your deployment method are unrelated. In fact, command-line deployment doesn't even use the CLI—it's done via regular git push. You can (and in my opinion should) have the CLI installed even if you are deploying via GitHub.
In this instance, you could use heroku run to create your Django superuser via a one-off dyno:
heroku run python manage.py createsuperuser
This is the most straightforward solution.
Here are some other reasons you might want to have the CLI installed:
To manage addons
To scale your app
To upgrade your app's stack
To configure multiple buildpacks, e.g. to use Node.js and Python in the same app
To view your app's logs
To enable HTTPS on your custom domain using Automated Certificate Management
To use the Docker registry and deployment
...
If you want to go down this path, install the CLI and then log in by running heroku login.
If there isn't a way without using the Heroku CLI, could someone tell me how I would init the Heroku git repository, considering the fact that I already have a GitHub repository that I have been using
After installing the CLI, cd into your project directory and tell Heroku which app that code belongs to by running heroku git:remote -a <your-app-name>.
This adds a Heroku remote but will not change any existing remotes. For example, if you were previously using git push origin to push to GitHub you'll still be able to do that.
After doing that you should be able to use heroku run as I showed above.

Heroku: installing Postgres without local db | Django

I've already pushed my Django files to Heroku via Git and now I'd like to configure at Heroku server the Postgres database. After that, I need to transfer my local sqlite database to Heroku Postgres.
All of this issue is because I don't have admin rights on my local PC (corporate unit) to install Postgres.
So basically:
1. Configure remotely Postegres at Heroku;
2. Migrate local database (sqlite) to Heroku (Postgres).
I don't know if there is another path to go on...
Thank you!
That's some crazy gymnastics you're trying to do. Getting a grasp of PostgreSQL setup is hard as it is. There are several user/table privileges you have to maintain. Sqlite3 has very simple settings in the settings.py file in Django, while Postgres requires a username, password, Host, and Port.
Also the sql you export from sqlite3, might not ingest directly into the PostgreSQL. See this. You'll have to install Postgres on your local machine, if you plan to have any sort of ok workflow as far as I can see. You can probably explore docker to create a local dev environment on your corporate PC.

Updating Django App on server

I am relatively new to Python/Django and have successfully deployed my first app. I want to update it now with some new changes, but I am not sure what the proper process is. My setup is ubuntu/nginx/gunicorn/postgres.
At the moment I am taking the following steps:
Stop nginx: sudo service nginx stop
Stop gunicorn: sudo service gunicorn stop
Backup the db? (not implemented - cant find it on the server)
Git Pull
python manage.py migrate
python manage.py collectstatic
restart gunicorn: sudo service gunicorn start
restart nginx: sudo service nginx restart
This is working, but I would appreciate some guidance if this is the complete, most accurate and safest way to do this please?
One lazy (yet recommended and professional) way of going about app updates is running automation script, like Fabric or Ansible.
However, if you wish to proceed the manual way (which is tedious), you might do something like:
Pull from git
Run migrations python manage.py migrate (This should ensure changes you made locally to your models reflect in production DB)
Run static collections to ensure new statics are reflected in server /static/ folder like so: python manage.py collectstatic
Then, restart your Django Server not Nginx. So something like: sudo service your_django_server_running_instance restart
On digitalOcean for instance (when used One-Click Install), your django server running instance is likely called gunicorn
Then you might want to look into automating your postgresql db as well