[Django]複数ページの移動

### テンプレート側
/hello/templates/hello/index.html

<body>
	<h1>{{title}}</h1>
	<p>{{msg}}</p>
	<p><a href="{% url goto %}">{{goto}}</a></p>
</body>
</html>

– {% %}はテンプレートタグ
– {% url ${name} %}で、urls.pyで指定しているnameのパスに遷移する

### views.py
/hello/views.py

def index(request):
	params = {
		'title':'Hello/Index',
		'msg':'this is sample page.',
		'goto':'next',
	}
	return render(request, 'hello/index.html', params)

def next(request):
	params = {
		'title':'Hello/Index',
		'msg':'this is another page.',
		'goto':'index',
	}
	return render(request, 'hello/index.html', params)

### urls.py
/hello/urls.py

urlpatterns = [ 
	path('', views.index, name='index'),
	path('next', views.next, name='next'),
]

http://192.168.33.10:8000/hello/

http://192.168.33.10:8000/hello/next

{% %}のテンプレートタグがやや特殊な動きをします。

[Django]テンプレートに値を渡す

### テンプレート側
/hello/templates/hello/index.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>{{title}}</title>
</head>
<body>
	<h1>{{title}}</h1>
	<p>{{msg}}</p>
</body>
</html>

### views.pyの修正
/hello/views.py

def index(request):
	params = {
		'title':'Hello/Index',
		'msg':'this is sample page.',
	}
	return render(request, 'hello/index.html', params)

paramsはkeyとvalueのdictionary

[Django]テンプレートの利用

### アプリケーションの登録
– プロジェクトフォルダのsettings.pyにアプリケーションを登録する
– INSTALLED_APPSに登録してtemplatesフォルダを検索できるようにする
/django_app/settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello', # 追加
]

### テンプレートフォルダの作成
/django_app/hello/templates/hello
– 他のアプリケーションと間違わないように、templatesの下にアプリケーション名のフォルダを作成する

### index.htmlを作成
/hello/templates/hello/index.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>hello</title>
</head>
<body>
	<h1>hello/index</h1>
	<p>This is sample page.</p>
</body>
</html>

/hello/urls.py

urlpatterns = [ 
	path('', views.index, name='index'),
]

/hello/views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
	return render(request, 'hello/index.html')

– render はテンプレートをレンダリングするのに使われる

http://192.168.33.10:8000/hello/

ほう、少し楽しくなってきました。

[Django]クエリパラメータを使用する

### クエリパラメータを記述する
クエリパラメータとはアドレスの後につけて記述するパラメータの事
e.g. http://hoge.com/hello/index?xxx=yyyy&zzz=aaa… など

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
	msg = request.GET['msg']
	return HttpResponse('you typed : "' + msg +'".')

– request.GET[‘&{param}’]でGETパラメータを取り出す。
– リクエストはHttpRequestクラスを使用し、レスポンスはHttpResponseクラスを使用する

### クエリパラメータがない時
MultiValueDictKeyErrorになるので、views.pyでmsgがない時の処理を追加する

/hello/views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
	if 'msg' in request.GET:
		msg = request.GET['msg']
		result = 'you typed : "' + msg +'".'
	else:
		result = 'please send msg parameter!'
	return HttpResponse(result)

– GETプロパティに設定されている値はQueryDictというクラスのインスタンス

### クエリパラメーターをスラッシュ(“/”)に変更する
/hello/urls.py

urlpatterns = [ 
	path('<int:id>/<nickname>', views.index, name='index'),
]

/hello/views.py

def index(request, id, nickname):
	result = 'your id: ' + str(id) + ', name: "' \
		+ nickname + '".'
	return HttpResponse(result)

– 文末のバックスラッシュ(“\”)は見かけの改行

[Django]アプリケーション内にurls.py新規作成

プロジェクト内のurls.pyではなく、アプリケーション内にurls.pyを新規作成しルーティングを書いていくのが一般的

### urls.pyの新規作成
helloフォルダ内にurls.pyを作成する

from django.urls import path
from . import views

urlpatterns = [   # path 関数で値を記述していく
	path('', views.index, name='index'), # 第一引数がアドレス、第二引数がviewsの実行関数、第三引数が実行
]

### django/urls.pyの新規作成
hello内のurls.pyを読み込むように修正

from django.contrib import admin
from django.urls import path, include # includeは引数に指定したモジュールを読み込む

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', include('hello.urls')), # path(アドレス、呼び出す処理)
]

表示が変わらないことを確認します
djangoにはプロジェクトとアプリケーションという概念があるようです

[Django]viewの作成方法

### アプリケーションを作成
$ python manage.py startapp hello

– migrationフォルダ: データベース関連の機能
– __initi__.py : アプリケーションの初期化処理を行うスクリプトファイル
– admin.py : 管理者ツールのため
– apps.py : アプリケーション本体の処理
– models.py : モデルに関する処理を記述
– tests.py : プログラムのテストに関するもの
– views.py : 画面表示

### views.py
/hello/view.py

from django.shortcuts import render
from django.http import HttpResponse # HttpResponseクラスをimport

def index(request): # requestはHttpResponseクラスのインスタンス
	return HttpResponse("Hello Django!!")

# Create your views here.

urlpatternsに登録した情報を元にどのアドレスにアクセスしたらどの処理が呼び出されるか決まる

### urls.py
/django_app/urls.py

from django.contrib import admin
from django.urls import path
import hello.views as hello

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', hello.index), # path(アドレス、呼び出す処理)
]

$ python manage.py runserver 192.168.33.10:8000

第一印象としては、Laravelは直感的ですが、Djangoはより関数的な書き方のように感じます。

Djangoプロジェクトの中身とサーバ起動

### プロジェクトフォルダ
– __initi__.py : 初期化処理を行うスクリプトファイル
– asgi.py : ASGIという非同期Webアプリケーションの為のプログラム
– settings.py : プロジェクトの設定情報
– urls.py : URLを管理するファイル
– wsgi.py : 一般的なWebアプリケーションプログラム
– manage.py : 機能に関するファイル

### サーバー起動
$ python manage.py runserver
-> このサイトにアクセスできません

ん? vagrantだから、http://127.0.0.1:8000/ は通ってない?

公式tutorial01/を見ると、ipとポートを指定できるみたい。
再度実行
$ python manage.py runserver 192.168.33.10:8000

あれ?
DisallowedHost at / … が表示される。
settings.pyのALLOWED_HOSTS = []を変更する

ALLOWED_HOSTS = ['192.168.33.10']

$ python manage.py runserver 192.168.33.10:8000

OK^^

Djangoを始めよう

$ python -V
Python 3.8.0
$ sudo apt install python-pip
$ pip install Django==3.0.4
Traceback (most recent call last):
File “/usr/bin/pip”, line 9, in
from pip import main
ModuleNotFoundError: No module named ‘pip’

ん?
$ wget https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py

再度インストール
$ pip install Django==3.0.4

### make Django project
$ mkdir django
$ cd django
$ django-admin startproject django_app

おおおおおお、ファイル少ないな
テンション上がってきました

ubuntuでデフォルトのpathをpython3.8にしたい

### python3.8インストール
$ sudo apt install -y python3.8

### 入っているpython
$ python -V
Python 2.7.17
$ python3 -V
Python 3.6.9
$ python3.8 -V
Python 3.8.0

$ ll /usr/bin/python*
lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python -> python2.7*
lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python2 -> python2.7*
-rwxr-xr-x 1 root root 3637096 Apr 15 17:20 /usr/bin/python2.7*
lrwxrwxrwx 1 root root 9 Oct 25 2018 /usr/bin/python3 -> python3.6*
-rwxr-xr-x 1 root root 1018 Oct 28 2017 /usr/bin/python3-jsondiff*
-rwxr-xr-x 1 root root 3661 Oct 28 2017 /usr/bin/python3-jsonpatch*
-rwxr-xr-x 1 root root 1342 May 1 2016 /usr/bin/python3-jsonpointer*
-rwxr-xr-x 1 root root 398 Nov 15 2017 /usr/bin/python3-jsonschema*
-rwxr-xr-x 2 root root 4526456 Apr 18 01:56 /usr/bin/python3.6*
-rwxr-xr-x 2 root root 4526456 Apr 18 01:56 /usr/bin/python3.6m*
-rwxr-xr-x 1 root root 5203488 Oct 28 2019 /usr/bin/python3.8*
lrwxrwxrwx 1 root root 10 Oct 25 2018 /usr/bin/python3m -> python3.6m*

$ cd /usr/bin
$ sudo rm python
$ sudo ln -s python3.8* python
$ python -V
Python 3.8.0

グラフィックボードの構造とGPU

### グラフィックボードの構造
– グラフィックボード基板
– GPUチップ
– ビデオメモリ
– 冷却装置
– 電源端子
– 出力端子(DVI, HDMI, DisplayPort)

GPUの周りにあるのがビデオメモリ
それらを冷却するためにヒートシンク、パイプ、ファンがある
基本的にPCI-Expressバスから電力を供給するが、消費電力が大きいグラフィックボードは補助電源の為のコネクタがある

## GPU
– モデルビュー変換(モデルの回転・移動をしたりカメラから見た時の座標を求める)
– 射影変換(カメラから見た時の座標(3次元)から,画面に映せる座標(2次元))
– クリッピング・カリング(描画する必要のない座標にあったり,裏から見えていたりする三角形を弾く)
– ビューポート変換
– ラスタライズ

やっぱり集積回路を自由に扱えるようになりたいなー