前回の記事の続きになります。
Djangoを立ち上げる〜Hello,world表示までの自分用メモ
ローカル環境に保存していた勉強ログをQiitaにあげていく自分用のメモです
開発環境
OS:mac
エディタ:vscode
Django:4.1.0
Python:3.10.9
index.htmlを表示していく過程を記す
manage.pyがあるディレクトリにてテンプレートファイルをまとめる
templatesディレクトリを作成して、その中にindex.htmlを作成
<h1>Hello,world!</h1>とでも書いておく
templatesの位置を教えてあげる
setting.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'DIRS': [BASE_DIR,'templates'],#追加
},
]
views.py
views.py
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name='index.html'
プロジェクトのurls.pyにアプリの方のurls.pyを見るように設定
urls.py(プロジェクト)
from django.contrib import admin
from django.urls import path,include#
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('アプリ名.urls')),#
]
アプリのurls.pyを設定
urls.py(アプリ)
from django.urls import path
from .views import IndexView
urlpatterns = [
path('',IndexView.as_view(),name='index'),
]