$ source ~/django/bin/activate
$ cd TestProject
ファイル「TestProject/applications/urls.py」を作成
$ cd applications
$ touch urls.py
「TestProject/applications/urls.py」の内容
from django.urls import path
from . import views
app_name = 'applications'
urlpatterns = [
path('index/', views.IndexView.as_view(), name='index'),
]
「TestProject/applications/views.py」の内容
from django.shortcuts import render
# Create your views here.
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name = 'index.html'
ディレクトリ「TestProject/templates」を作成
$ cd ..
$ mkdir templates
作成したディレクトリ「TestProject/templates」を検索できるよう
に「TestProject/TestProject/settings.py」を編集する
編集前
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
編集後
先ず、1行目に「import os」を追加する
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
##'DIRS': [],
'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',
],
},
},
]
「TestProject/templates/index.html」を作成
$ touch templates/index.html
「TestProject/templates/index.html」の内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Test</title>
</head>
<body>
<h1>ジャンゴのテストじゃあ</h1>
</body>
</html>
「TestProject/TestProject/urls.py」を編集
編集前
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
編集後
from django.contrib import admin
##from django.urls import path
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('applications.urls')),
]
確認
CTRL+cで停止して、コマンド実行
$ python manage.py runserver 0.0.0.0:8000
で、他のPCでブラウザ起動、「http://サーバーIPアドレス:8000/index」
を入力して表示できればヨシ。
とりあえず、ココまで
CTRL+cで停止
$ deactivate