LoginSignup
0
3

More than 5 years have passed since last update.

DJANGO for BEGINNERSをやってみる その1

Last updated at Posted at 2018-12-20

はじめに

Django公式チュートリアルやったはいいけど、次何しようと思ってたので、
どこかで紹介されていたDJANGO for BEGINNERS(洋書)を読み進めることにする。
Chapter1はセットアップ,Chapter2はHello World的なものなので割愛。

Chapter3 : Pages app

ViewとTemplateだけ使った簡単なアプリを作る。
この章ではModelは触らない。

プロジェクト作成

適当な場所でプロジェクト作成する

start_project_command
 % mkdir pages
 % django-admin startproject pages_project .

Pages Appの認識

Pages Appを認識させるためにsettings.pyに追加する

pages_project/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pages.apps.PagesConfig', # 追加
]

認識できているか確認する。

runserver
 % python manage.py runserver

ブラウザからhttp://127.0.0.1:8000/ に接続してページが表示されればOK

Templates作成

ページの見た目を変えるためにtemplatesフォルダとhtmlファイルを以下の構造になるように作る

Layout
pages
  |- templates
         |- home.html

公式チュートリアルにもあったように、templatesのパスを追加しておく。

pages_project/settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')], # 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',
            ],
        },
    },
]

実際に表示する内容をhtmlに書く。
とりあえず1行だけ。

templates/pages/home.html
<h1> Pages App !! </h1>

これだけじゃ何も表示は変わらないのでViewを作る

Viewの作成

プロジェクト作成時にできているview.pyを編集する。
今回はクラスベース汎用ビューのTemplateViewを使う。
クラスベース汎用ビューが何かは以下の記事とかを参考に。
Djangoにおけるクラスベース汎用ビューの入門と使い方サンプル

pages/view.py
from django.shortcuts import render
from django.views.generic import TemplateView

# Create your views here.
class HomePageView(TemplateView):
    template_name = 'home.html'

次はViewとTemplateを繋げるためにURLsを編集する

URLsの編集

まずはすでにあるurls.pyを編集する

pages_project/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('pages.urls')), # サーバー接続時にpages/urls.pyをみるようになる
]

アプリ用のurls.pyを作る必要があるので、pages/urls.pyを作る

pages/urls.py
from django.urls import path

from .views import HomePageView # View Classのimport

urlpatterns = [
    path('', HomePageView.as_view(), name='home'), # これでサーバー接続時にViewClassで指定したtemplateが表示されるようになる
]

再びrunserverでサーバー起動してから http://127.0.0.1:8000/ に接続して確認する

About Pageの追加

home.html追加した手順と同様に、about.htmlを作成し、View,URLsを追加する。
手順は省いて、結果だけ載せる。

Template

Layout
pages
  |- templates
         |- home.html
         |- about.html

View

pages/view.py
from django.shortcuts import render
from django.views.generic import TemplateView

# Create your views here.
class HomePageView(TemplateView):
    template_name = 'home.html'

class AboutPageView(TemplateView):
    template_name = 'about.html'

URLs

pages/urls.py
from django.urls import path

from .views import HomePageView # View Classのimport

urlpatterns = [
    path('', HomePageView.as_view(), name='home'), 
    path('about/', AboutPageView.as_view(), name='about'), 
]

最後に以下のページ接続して作成したhtmlが表示されれば成功!
http://127.0.0.1:8000/about/

続き

DJANGO for BEGINNERSをやってみる その2

0
3
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
3