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 HTMLテンプレートでHello Worldを表示する

Last updated at Posted at 2025-08-29

はじめに

以前、DjangoでHello Worldをした。
その際、HTTPのレスポンスでHello World文字列を返すようにしていたので、今回は、HTMLテンプレートを使ってHello Worldを返す。
少しずつWebアプリっぽくしていこう。

URLマッピング追加

まずは、プロジェクトのurls.pyにURLマッピングを追加する。

path("hello/,"views.say_hello),

テンプレートファイル追加

template/helloWorld/hello.html

<!DOCTYPE html> 
<html>
<head>
    <title>Hello Django</title>
</head>
<body>
    <h1>元気ですか?、{{name}}さん!</h1>
</body>
</html>

サーバー起動

サーバーの起動は、dockerではなく、Django開発用のWebサーバーを使う。

python manage.py runserver 0.0.0.0:8000

dockerでサーバー起動する場合は、ファイルの同期をしないといけない。

docker run -p 8000:8000 -v .:app my-django-app

実行してみたが、TemplateDoesNotExist になってしまう。
→settings.pyのINSTALLED_APPSに今回追加したアプリを追加する必要があった。

INSTALLED_APPS = [
    'helloWorld',
]

image.png

コミット

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?