---
# System prepended metadata

title: Тема 12. Развертывание на Heroku
tags: [Python Web]

---

---
tags: Python Web
source: https://python-scripts.com/doska-obyavlenij-django
---

# Тема 12. Развертывание на Heroku

https://github.com/roman-yatsenko/django-news-heroku

https://arcane-castle-94924.herokuapp.com/ (reset every 24h)

## Heroku

1. Зарегистрироваться на https://www.heroku.com/
2. Установить [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli)

```shell
heroku login
pipenv lock
touch Procfile
```

### `Procfile`

```
web: gunicorn news_project.wsgi --log-file -
```

```shell
pipenv install gunicorn
```

### `pages_project\settings.py`

```python=28
ALLOWED_HOSTS = ['*']
```

### `.gitignore`

```
# db.sqlite3
```

Commit & push

## Deploy

```shell
heroku create
# get <appname>
heroku git:remote -a <appname>
heroku config:set DISABLE_COLLECTSTATIC=1
git push heroku main
heroku ps:scale web=1
heroku open
```

## Статические файлы

```shell
pipenv install whitenoise
```

### `settings.py`

```python
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'whitenoise.runserver_nostatic', # new!
    'django.contrib.staticfiles',
    'blog',
    'accounts',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', # new!
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

...

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') # new!
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' # new!
```

---

(c) Яценко Р.Н., 2021

[Учебный центр компьютерных технологий "Кит"](http://kit.kh.ua/)

<img src="https://i.imgur.com/Kh901c1.png" style="width: 150px; position: fixed; top: 100px; right: 10px; border: 0; box-shadow: none;">
