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 環境構築 #2 HTMLを表示できる環境を作る

Posted at

前回から間が空いたが、前回の続きを投稿してみる。

今回は、HTMLを無事に表示する所まで。

前回までの記事

今回使うファイル

    プロジェクトフォルダ
  • settings.py
  • urls.py
    アプリケーションフォルダ
  • views.py
  • urls.py 各自で用意する。

補足事項

  • アプリケーションフォルダと書いてある時、 __init__.pyがあるフォルダのことを指している。

  • プロジェクトフォルダと書いてある時、 settings.pyがあるフォルダのことを指している。

この中に挙げているファイルは前々回で自動生成されるファイルなので、よっぽど何かない限りは動かさない

というか動かす事殆どないはずだから、、。

というわけで始める。

templates フォルダをアプリケーションフォルダの下に作る

アプリケーションフォルダ-----    __init__.py 
                           、、、、
                        、、、、、、
                        templates---app---- test.html

appフォルダはなくてもおk。

2.settings.py にテンプレートフォルダを教える

#こんな感じ
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

os.pas.joinメソッドの引数に文字列でフォルダ名を与える事

3. views.pyの記述

#ファイル名は何でも良い

この3つは必ずインポートする
from django.shortcuts import render,redirect  #--A
from django.template import loader             #--B
from django.http import HttpResponse          #--C



def test(request):
    #表示したいhtmlを値に指定する

   #B 表示したいHTMLを指定
    template = loader.get_template('app/test.html')
 

    msg = 'これでもOK';

    
    context = {'message' :'テスト','msg':msg}

            #C                               #B
    return HttpResponse(template.render(context,request))

リクエストでurlを指定するとこの関数が実行されると

 レスポンスでhtmlが返ってくる

4. urls.pyの記述

こっちはアプリケーションフォルダ側
from django.urls import include,path

from .test_views  import test  #ここでビューのurlを指定している

app_name = 'Display'

#右からurl、関数名、 テンプレートに渡す関数のaliasイベント発火の時に必要
urlpatterns = [
           path('test/',test)
}



#http://127.0.0.1:8000/app_nameの文字列/path関数の第一引数文字列
とリクエストする事になる

path関数について少し説明

  • 第一引き数はurl
  • 第二引き数はviews.pyで記述した関数名
  • 第3引数は簡単に言うと要素に与える名前。今回は使わないので割愛。

※関数名に()を付けないようにする事

5. urls.pyの記述

こっちはプロジェクト側
from django.contrib import admin
from django.urls import include,path 


urlpatterns = [
    path('admin/', admin.site.urls),

    #ここにないとリクエストした時エラーになる
    path('アプリケーションフォルダ名/',include('アプリケーションフォルダ名.urls'))
]

htmlを最後に

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    {% comment %} viewsで指定した値をここに渡す {% endcomment %}
    <h1>{{message}}</h1>
    <h2>{{msg}}</h2>
</body>

</html>

実行する

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?