how to deploy to heroku djangorest framework app
To deploy a Django app with Django REST Framework to Heroku, you can follow these steps:
Create a Heroku Account: If you don't have one already, sign up for a free Heroku account at heroku.com.
Install the Heroku CLI: Download and install the Heroku CLI, which provides a command-line interface for deploying apps to Heroku.
Set up a Git repository: Initialize a Git repository in your Django project folder by running the command
git init
.Create a Heroku app: In your terminal, navigate to your project folder and run
heroku create
to create a new Heroku app. This will also add a Git remote to your repository.Configure the app: Set the
ALLOWED_HOSTS
setting in your Django settings file to include your Heroku app's domain. You can find this by runningheroku apps:info
in your terminal.Install required dependencies: Add
gunicorn
andpsycopg2
(if you're using PostgreSQL) to yourrequirements.txt
file. These are required for running your app on Heroku.Create a
Procfile
: In the root of your project directory, create a file calledProcfile
(without any file extension) and add the following line:web: gunicorn your_project_name.wsgi
.Commit and push your code: Commit your changes to Git by running
git add .
followed bygit commit -m "Initial commit"
. Then, push your code to the Heroku remote repository usinggit push heroku master
.Set up database: If you're using a database, configure it on Heroku. For example, if you're using PostgreSQL, run
heroku addons:create heroku-postgresql:hobby-dev
to add a PostgreSQL database to your app.Migrate your database: Run
heroku run python manage.py migrate
to apply any pending database migrations on Heroku.Scale your app: By default, Heroku assigns only one web dyno to your app. To scale it, run
heroku ps:scale web=1
.Visit your app: Finally, run
heroku open
to open your app in a browser and verify that it's working.
That's it! Your Django app with Django REST Framework is now deployed on Heroku.