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?

DjangoでHello Worldをする

Last updated at Posted at 2025-08-17

目的

pythonでWebアプリを作りたい。
Djangoを使って、とりあえずHello Worldしてみる。
ゆくゆくはDockerコンテナで動かしたい。

Djangoアプリの作成

以下サイトの通りに設定をする

結局やることは、仮想環境を作って、Djangoをインストール
VSCodeで、Bashを使いコマンドを打っていく。

py -m venv venv
# 仮想環境を有効化してからDjangoのインストールをする
source venv/Scripts/activate
py -m pip install Django

Djangoプロジェクト作成

django-admin startproject mysite .

Djangoアプリ作成

python manage.py start app helloWorld

URLルーティング追加

ブラウザからのリクエストをアプリに誘導する

プロジェクトのURL設定

mysite/urls.py にアプリ側へのルーティングを追加

# 追加行
path("",include("helloWorld.urls")),

アプリのURL設定

ファイル自体の追加が必要

from django.urls import path
from . import views

urlpatterns = [
    path("", views.index, name="index"),
]

HTTPレスポンスを返す

viewsの中に、Hellow World文字列を返すindex関数を追加する。

from django.http import HttpResponse

# Create your views here.
def index(request):
    return HttpResponse("Hello World")

サーバーを起動する。

ブラウザからlocalhost:8000でアクセスできるようになるはず。

python manage.py runserver

結果

本当にただ表示されるだけだが、できた。
image.png

gitで管理する

.gitignoreは以下の記事を参考にした。

以下リポジトリに追加
https://github.com/moto0206/django-hello

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?