0
0

More than 3 years have passed since last update.

Djangoいまさらまとめ

Last updated at Posted at 2020-05-06

いまさらのDjangoまとめ

Web開発初心者。
チュートリアルを読み下した自分用メモ。

Django

ジャンゴと発音。
※python3.6以降をサポートしている。

  • pythonのウェブフレームワークの一つ
  • ウェブフレームワークとは webサービスの実装を手助けしてくれるテンプレートのこと。 ウェブサービスを作りやすいように、 ウェブフレームワーク側でいろいろな機能を提供してくれている。 Djangoの場合DBとやり取りが楽に書けるらしい。 ただし、制約:階層やファイル名の規則が定義されており、ちゃんと従わないと動かない。

WSGI(web server gateway interface)

WebアプリケーションとWebサーバを接続するためのインターフェース
Apacheの代わりにWSGI使う。
Webサーバ上ではこれでDjangoを実行する。
本環境ではGunicornを使用

Django標準のDB:sqlite3

Djangoではクエリ文がほぼ隠蔽されてる
代わりにmodelでデータ型を定義
他にもPostgreSQL,MySQL,Oracleが使用できる
DBを冗長化させるにはこちらを使用するべきか
本環境ではSQLiteを使用

使い方

$ django-admin startproject mysite #step1:プロジェクト作成
$ cd mysite ## 作成したdjangoのプロジェクトに入ったら、manage.pyがあるはず。そのdirまで移動。基本操作はmanege.pyで実施
$ python manage.py startapp polls #pollsはサーバ名、公式から流用 #step2:開発用サーバ作成

フォルダ構成

公式Documentの例を引用する。

  • mysite/
    • manage.py
    • mysite/
      • init.py
      • settings.py #設定統括。アプリケーションやmodelが何かをここで定義必要
      • urls.py
      • asgi.py
      • wsgi.py
    • polls
      • init.py
      • admin.py
      • apps.py
      • migrations/
        • init.py
      • models.py #データベースのレイアウトとそれに付随するメタデータ
      • tests.py #テストはここ
      • views.py
      • url.py #URLconfを記載自分で作成必要、ここにも作っておいたほうがわかりやすい)

url定義

url内の'url_pattern'リスト内にpath関数で指定する。

polls/url.py
path('<int:question_id>/', views.huga, name='detail') #ここで指定した文字列をurlの名前としてhtml内でも使える。

これを、htmlファイル内でこう表現することができる。

<li><a href="{% url 'detail' question_id %}">{{ question.question_text }}</a></li>

実際は、一つのプロジェクト内で複数アプリがあるため、まずそこで名前空間を分けて、全体で見てユニークになるようにする。

polls/url.py
appname = 'polls'
urlpatterns = [
    path('<int:question_id>/', views.huga, name='detail'),
    ]
<li><a href="{% url 'polls:detail' question_id %}">{{ question.question_text }}</a></li>

djangoサーバ操作

$ python manage.py runsever #開発用サーバ起動。本番用サーバはWSGIで起動させるので違うコマンドになる
$ python manage.py migrate #INSTALLED_APPSを参照&settings.pyファイルのDB設定に従ってデータベーステーブルを作成
$ python manage.py makemigrations polls #モデルの変更をDBに反映する
$ python manage.py createsuperuser #管理用ページのユーザを作成する

HTTPレスポンス作成

polls/views.py
from django.http import HttpResponse
from django.template import loader
from .models import Question

def index(request):
    latest_question_list = Question.object.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = {'latest_question_list':latest_question_list}
    return HttpResponse(template.render(context, request))

HTTPレスポンスはrender関数を使って書ける

polls/views.py
from django.shortcuts import render
from .models import Question

def index(request):
    latest_question_list = Question.object.order_by('-pub_date')[:5]
    context = {'latest_question_list':latest_question_list}
    return render(request, 'polls/index.html' , context)

一旦ここまで。つづきは汎用ビューとかから

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