LoginSignup
7
16

More than 1 year has passed since last update.

Djangoで作成したWebアプリケーションを無料で公開する(PythonAnywhere)

Last updated at Posted at 2022-07-03

1. はじめに

前回の「DjangoでHello Worldを表示する」で作成したWebアプリケーションをPythonAnywhereというサービスを使用して無料で公開します。

2. PythonAnywhereとは

PythonAnywhereはDjangoやFlaskなどのPythonのWebフレームワークに特化したホスティングサービスです。無料プランがあるため勉強用やお試しで利用することができます。無料プランだと制限は多いですが、有料プランに切り替えることで実運用も可能になります。

3. デプロイ

GitHubとPythonAnywhereを連携してデプロイします。

3.1 GitHubにソースコードをプッシュ

まず、GitHubにリポジトリを作成し、下記のコマンドでソースコードをプッシュします。
コマンドは前回作業していた一番上のmysiteフォルダに移動して実行します。

$ git init
$ git config --global user.name 【ユーザ名】
$ git config --global user.email 【メールアドレス】
$ git add --all .
$ git commit -m "My Django app, first commit"
$ git remote add origin https://github.com/<your-github-username>/<repositoryname>.git
$ git push -u origin master

3.2 PythonAnywhereの無料プランのアカウント作成

PythonAnywhereの無料プランのアカウントを作成します。
登録はユーザ名、メールアドレス、パスワードのみです。クレジットカードなどの登録は不要です。

3.3 PythonAnywhere APIトークンの作成

次にPythonAnywhereにサインインした後、APIトークンを作成しておきます。
画面右上のAccountをクリックし、AccountページのAPI Tokenタブで「Create a new API token」をクリックして作成します。

1.png

2.png

3.4 PythonAnywhere でサイトを設定

PythonAnywhereのコンソールでサイトの設定していきます。
まず、pipでpythonanywhereのパッケージをインストールします。

$ pip3.9 install --user pythonanywhere

次に下記のコマンドを実行し、サイトの設定します。
GitHubからクローンし、Webアプリケーションの公開まですべて自動で行ってくれます。

$ pa_autoconfigure_django.py --python=3.9 https://github.com/<your-github-username>/<repositoryname>.git

最後に管理画面にログインできるようにsuperuser(サイトの全てを管理するユーザー)を作成します。

$ python manage.py createsuperuser

3.6 動作確認

作成したWebアプリケーションが公開されているか確認します。
無料プランの場合、ドメインは「ユーザ名.pythonanywhere.com」になっています。

管理画面(https://ユーザ名.pythonanywhere.com/admin/)
管理画面.png

Hello Worldのページ(https://ユーザ名.pythonanywhere.com/hello/)
hello_.png

4. おわりに

今回はDjangoで作成したWebアプリケーションを無料で公開してみました。
Djangoには多くの機能があるので勉強してどんどん追加していこうと思います。

7
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
16