Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Deploying Your Django app on Heroku

$
0
0

Heroku is a cloud application platform it's a new way of building and deploying web apps, Which makes easy to host your application in the cloud with a simple git push command.

Heroku supports several programming languages like(python, Java, php)

Install the Heroku Toolbelt:

The first thing you need to do is to install the Heroku toolbelt. The toolbelt is a command line software which makes it easy to interact with the Heroku service through your command line.

Here you can find out the Heroku toolbelt installation for Debian/Ubuntu, Run this from your terminal:

wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh

In the below link you need to select your required operating system(Download Heroku Toolbelt for..) for installing heroku toolbelt in your system. And then you can proceed to install.

Please click here

to install Heroku ToolBelt

After successfully installation of toolbelt, you can use the heroku command from your terminal.

heroku login
You will prompt to provide heroku credentials(Email and password), once you have authenticated you can access both heroku and git commands. Create your heroku app:

The following command is used to create an app

heroku create your-app-name

Here 'your-app-name' should be unique, heroku will generate default app-name if you won't specify any app-name.

For creating remote for your app use this command

heroku git:remote -a your-app-name Define a Procfile:

A Procfile is a mechanism for declaring what commands need to run by your application dynos on the Heroku platform.

Use a Procfile, a text file in the root directory of your application, to explicitly declare what command should be executed to start your app.

Here first you need to install gunicorn using pip

pip install gunicorn
web: gunicorn yoursettings.wsgi --log-file -

Create new file in the root directory of your application as "app.json"

{
"name": "Your-App-Name",
"description": "Self-hosted app",
"repository": "provide your git repository url",
"scripts": {
"postdeploy": "python manage.py makemigrations && python manage.py migrate"
}
}

"app.json" is a json format file for describing web apps. It declares environment variables, Description, and other information required to run an app on Heroku.

To Load your app Static files you need to use "WhiteNoise", this can be install by using pip

pip install whitenoise

And also you need to update your "wsgi.py" file too

from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Update STATIC_ROOT in your "settings.py" file for loading your static files

STATIC_ROOT = (os.path.join(BASE_DIR, "static")) Database Configuration:

For configuring your database, First you need to install "dj_database_url" using pip

pip install dj_database_url
pip install psycopg2
import dj_database_url
DATABASES = {
'default': dj_database_url.config(
default='sqlite:////{0}'.format(os.path.join(BASE_DIR, 'db.sqlite3'))
)
} Dependecies used: 1. dj-database-url
2. Django
3. gunicorn
4. psycopg2
5. whitenoise

Update all your app dependencies in your "requirements.txt" file which is in the root directory of your app. These are the basic dependencies used.

Deploying Your app on Heroku:

This can be done by using simple git command

* It will read yourDjango appication

* Install your requirements provided

* Detects your Procfile

* It will collect your static files.

git push heroku master

After Sucessfully deploying your app, you will be provided with the url to open your app.

you can also run it by using the command.

heroku open


Viewing all articles
Browse latest Browse all 9596

Trending Articles