0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python migrateとWebサーバーの起動をターミナル上から一括で行う【備忘録】

Posted at

全文

param(
    $Args1,
    [switch]$Activate
)

# 仮想環境のパス (必要に応じて変更してください)
$venvPath = ".\venv\Scripts\Activate.ps1"

# 仮想環境の有効化
if ($Activate -or (Test-Path $venvPath)) {
    Write-Host "仮想環境を有効化中..." -ForegroundColor Cyan
    # ドットソーシングで仮想環境を現在のセッションに読み込む
    . $venvPath
    if (-not $?) {
        Write-Host "エラー: 仮想環境の有効化に失敗しました。" -ForegroundColor Red
        exit 1
    }
} else {
    Write-Host "仮想環境の有効化スキップ。--Activateスイッチを使用するか、パスを確認してください。" -ForegroundColor Yellow
}

# データベースマイグレーションの実行
Write-Host "マイグレーションを実行中..." -ForegroundColor Green
python manage.py migrate

if (-not $?) {
    Write-Host "エラー: マイグレーションに失敗しました。" -ForegroundColor Red
    exit 1
}

# ブラウザを自動的に立ち上げて指定URLへ移動
if ($Args1) {
    Start-Process "http://127.0.0.1:8000/$Args1"
} else {
    Start-Process "http://127.0.0.1:8000/"
}

# 開発サーバーの起動
Write-Host "開発サーバーを起動中..." -ForegroundColor Blue
python manage.py runserver

使用法

・任意のディレクトリにファイルを配置(私の場合はルートディレクトリ)
・「start_server.ps1 」という名前で保存

.\start_server.ps1 

※ ルートディレクトリに保存した場合。任意の場所に保存した場合はパスを編集してください。

Webブラウザの自動起動

.\start_server.ps1 index

・第一引数に任意のURLを入力するとそのページにアクセスが出来ます。
・URLPrefixは「http://127.0.0.1:8000」なので仮想環境はすぐにアクセスできます。ステージング環境や本番環境で使いたいときはURLプレフィックスを変更してください。
・第二引数にどの環境で起動するかを制御する変数を定義してURLが分岐する実装にしても良いかもしれません。

使用例

.\start_server.ps1 users/21001
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?