[Django]ジェネリックビュー

ジェネリックビューは指定したモデルから全レコードを取り出したり、特定のIDのものだけ取り出す基本的機能を持ったビュー
ListViewは全レコードを取り出すためのジェネリックビュー
DetailViewは特定のレコードを取り出すためのジェネリックビュー

### ジェネリックビューで表示
/hello/views.py

from django.views.generic import ListView
from django.views.generic import DetailView

class FriendList(ListView):
	model = Friend

class FriendDetail(DetailView):
	model = Friend

/hello/urls.py

from .views import FriendList
from .views import FriendDetail

urlpatterns = [ 
	path('', views.index, name='index'),
	path('create', views.create, name='create'),
	path('edit/<int:num>', views.edit, name='edit'),
	path('delete/<int:num>', views.delete, name='delete'),
	path('list', FriendList.as_view()),
	path('detail/<int:pk>', FriendDetail.as_view()),
]

/hello/templates/hello/friend_list.html

<body class="container">
	<h1 class="display-4 text-primary">Friends List</h1>
	<table class="table">
		<tr>
			<th>id</th>
			<th>name</th>
			<th></th>
		</tr>
	{% for item in object_list %}
		<tr>
			<td>{{item.id}}</td>
			<td>{{item.name}}</td>
			<td><a href="/hello/detail/{{item.id}}">detail</a></td>
		</tr>
	{% endfor %}
	</table>
</body>

/hello/templates/hello/friend_detail.html

<body class="container">
	<h1 class="display-4 text-primary">Friends Detail</h1>
	<table class="table">
		<tr>
			<th>id</th>
			<th>{{object.id}}</th>
		</tr>
		<tr>
			<th>name</th>
			<th>{{object.name}}</th>
		</tr>
		<tr>
			<th>mail</th>
			<th>{{object.mail}}</th>
		</tr>
		<tr>
			<th>gender</th>
			<th>{{object.gender}}</th>
		</tr>
		<tr>
			<th>age</th>
			<th>{{object.age}}</th>
		</tr>
	</table>
</body>

なるほど

[Django]CRUDのDelete

レコードのモデルインスタンスを取得してdeleteメソッドを実行する

urlspaternsの追記
/hello/urls.py

urlpatterns = [ 
	path('', views.index, name='index'),
	path('create', views.create, name='create'),
	path('edit/<int:num>', views.edit, name='edit'),
	path('delete/<int:num>', views.delete, name='delete'),
]

index.htmlの修正
/hello/templates/hello/index.html

{% for item in data %}
		<tr>
			<td>{{item}}</td>
			<td><a href="{% url 'edit' item.id %}">Edit</a></td>
			<td><a href="{% url 'delete' item.id %}">Delete</a></td>
		</tr>
	{% endfor %}

delete.htmlの作成
/hello/templates/hello/delete.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>※以下のレコードを削除します。</p>
	<table class="table">
		<tr>
			<th>ID</th>
			<td>{{obj.id}}</td>
		</tr>
		<tr>
			<th>Name</th>
			<td>{{obj.name}}</td>
		</tr>
		<tr>
			<th>Gender</th>
			<td>
				{% if obj.gender == False %} male {% endif %}
				{% if obj.gender == True %} female {% endif %}
			</td>
		</tr>
		<tr>
			<th>Email</th>
			<td>{{obj.mail}}</td>
		</tr>
		<tr>
			<th>Age</th>
			<td>{{obj.age}}</td>
		</tr>
		<tr>
			<th>Birth</th>
			<td>{{obj.birthday}}</td>
		</tr>
		<form action="{% url 'delete' id %}" method="post">
		{% csrf_token %}
		<tr><th></th><td>
			<input type="submit" value="click" class="btn btn-primary">
		</td></tr>
	</form>
	</table>
</body>

delete関数を作る
/hello/views.py

def delete(request, num):
	friend = Friend.objects.get(id=num)
	if(request.method == 'POST'):
		friend.delete()
		return redirect(to='/hello')
	params = {
		'title': 'Hello',
		'id': num,
		'obj': friend,
	} 
	return render(request, 'hello/delete.html', params)

CRUDの基本は抑えました。

[Django]CRUDのUpdate

/hello/urls.py

urlpatterns = [ 
	path('', views.index, name='index'),
	path('create', views.create, name='create'),
	path('edit/<int:num>', views.edit, name='edit'),
]

/hello/templates/hello/create.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<table class="table">
		<tr>
			<th>data</th>
		</tr>
	{% for item in data %}
		<tr>
			<td>{{item}}</td>
			<td><a href="{% url 'edit' item.id %}">Edit</a></td>
		</tr>
	{% endfor %}
	</table>
</body>

– url ‘edit’ item.idで/edit/1というようにアクセスできるようになる

edit.htmlを作る
/hello/templates/hello/edit.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<form action="{% url 'edit' id %}" method="post">
		{% csrf_token %}
		<table class="table">
		{{ form.as_table }}
		<tr>
			<td></td>
			<td><input type="submit" value="click" class="btn btn-primary mt-2"></td>
		</tr>
	</table>
	</form>
</body>

edit関数を作る
/hello/views.py

# edit
def edit(request, num):
	obj = Friend.objects.get(id=num)
	if(request.method == 'POST'):
		friend = FriendForm(request.POST, instance=obj)
		friend.save()
		return redirect(to='/hello')
	params = {
		'title': 'Hello',
		'id': num,
		'form': FriendForm(instance=obj),
	}
	return render(request, 'hello/edit.html', params)

引っ張る時は、’form’: FriendForm(instance=obj),として、updateの時はFriendForm(request.POST, instance=obj)とするのか。なお、createの時はobj = Friend()

[Django]ModelFormを使う

ModelFormを使うことでよりスムーズにレコードの保存を行うことができる

/hello/forms.py

from django import forms
from.models import Friend

class FriendForm(forms.ModelForm):
	class Meta:
		model = Friend
		fields = ['name','mail','gender','age','birthday']

/hello/views.py

def create(request):
	if(request.method == 'POST'):
		obj = Friend()
		friend = FriendForm(request.POST, instance=obj)
		friend.save()
		return redirect(to='/hello')
	params = {
		'title': 'Hello',
		'form': HelloForm(),
	}
	return render(request, 'hello/create.html', params)

– obj = Friend()でインスタンスを作成し、FriendForm(request.POST, instance=obj)インスタンスを作成する

/hello/templates/hello/create.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<form action="{% url 'create' %}" method="post">
		{% csrf_token %}
		<table class="table">
		{{ form.as_table }}
		<tr>
			<td></td>
			<td><input type="submit" value="click" class="btn btn-primary mt-2"></td>
		</tr>
	</table>
	</form>
</body>

先が長いな。

[Django]CRUDのCreate

/hello/forms.py

from django import forms

class HelloForm(forms.Form):
	name = forms.CharField(label='Name', widget=forms.TextInput(attrs={'class':'form-control'}))
	mail = forms.EmailField(label='Email', widget=forms.EmailInput(attrs={'class':'form-control'}))
	gender = forms.BooleanField(label='Gender', widget=forms.CheckboxInput(attrs={'class':'form-check'}))
	age = forms.IntegerField(label='Age', widget=forms.NumberInput(attrs={'class':'form-control'}))
	birthday = forms.DateField(label='Birth', widget=forms.DateInput(attrs={'class':'form-control'}))

### create.htmlの作成
/hello/templates/hello/create.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>{{title}}</title>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" crossoorigin="anonymous">
</head>
<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<form action="{% url 'create' %}" method="post">
		{% csrf_token %}
		{{ form.as_p }}
		<input type="submit" value="click" class="btn btn-primary mt-2">
	</form>
</body>
</html>

/hello/templates/hello/index.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<table class="table">
		<tr>
			<th>data</th>
		</tr>
	{% for item in data %}
		<tr>
			<td>{{item}}</td>
		</tr>
	{% endfor %}
	</table>
</body>

/hello/views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import redirect
from .models import Friend
from .forms import HelloForm

def index(request):
	data = Friend.objects.all()
	params = {
		'title': 'Hello',
		'data': data,
	}		
	return render(request, 'hello/index.html', params)

# create model
def create(request):
	params = {
		'title': 'Hello',
		'form': HelloForm(),
	}
	if(request.method == 'POST'):
		name = request.POST['name']
		mail = request.POST['mail']
		gender = 'gender' in request.POST
		age = int(request.POST['age'])
		birth = request.POST['birthday']
		friend = Friend(name=name, mail=mail, gender=gender, age=age, birthday=birth)
		friend.save()
		return redirect(to='/hello')
	return render(request, 'hello/create.html', params)

– models.pyで定義しているFriendインスタンスを作成して、インスタンスを保存する
– return redirectでリダイレクト

/hello/urls.py

from django.urls import path
from . import views

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

流れとしては、create.htmlからcreate.htmlにpostして、modelのインスタンスを使ってsave()してリダイレクトさせる
リダイレクト先で、if request.method == ‘POST’としても同じだろう。

[Django]モデルのカスタマイズ

### モデルのリストを調査
/hello/views.py

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

def index(request):
	params['data'] = Friend.objects.all()
	params = {
		'title': 'Hello',
		'data': data,
	}		
	return render(request, 'hello/index.html', params)

/hello/templates/hello/index.html

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

def index(request):
	data = Friend.objects.all()
	params = {
		'title': 'Hello',
		'data': data,
	}		
	return render(request, 'hello/index.html', params)

– .objects.all()で取り出していたのはQuerySetというインスタンスだということがわかる
– QuerySetには__str__も含む

### valuesメソッド
– .objects.values()で辞書でデータを取り出すことができる
/hello/views.py

def index(request):
	data = Friend.objects.values()
	params = {
		'title': 'Hello',
		'data': data,
	}		
	return render(request, 'hello/index.html', params)

– 特定の項目を指定

def index(request):
	data = Friend.objects.values('id', 'name')
	params = {
		'title': 'Hello',
		'data': data,
	}		
	return render(request, 'hello/index.html', params)

– リストとして取り出す

data = Friend.objects.all().values_list('id', 'name','age')

– first最初、last最後、countレコード数

def index(request):
	num = Friend.objects.all().count()
	first = Friend.objects.all().first()
	last = Friend.objects.all().last()
	data = [num, first, last]
	params = {
		'title': 'Hello',
		'data': data,
	}		
	return render(request, 'hello/index.html', params)

– QuerySetのカスタマイズ

from django.shortcuts import render
from django.http import HttpResponse
from .models import Friend
from django.db.models import QuerySet

def __new_str__(self):
	result = ''
	for item in self:
		result += '<tr>'
		for k in item:
			result += '<td>' + str(k) + '=' + str(item[k]) + '</td>'
		result += '</tr>'
	return result

QuerySet.__str__ = __new_str__

def index(request):
	data = Friend.objects.all().values('id', 'name', 'age')
	params = {
		'title': 'Hello',
		'data': data,
	}		
	return render(request, 'hello/index.html', params)
<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<table class="table">
		{{data|safe}}
	</table>
</body>

reverseなどもviews.pyで整形すれば良いのですね、少しイメージが湧いてきました。

[Django]指定IDのレコードを取得

### 特定のIDのみ取り出す
/hello/forms.py

from django import forms

class HelloForm(forms.Form):
	id = forms.IntegerField(label='ID')

/hello/templates/hello/index.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p class="h5 mt-4">{{message|safe}}</p>
	<form action="{% url 'index' %}" method="post">
		{% csrf_token %}
		{{ form}}
		<input type="submit" value="click">
	</form>
	<hr>
	<table class="table">
		<tr>
			<th>ID</th>
			<th>NAME</th>
			<th>GENDER</th>
			<th>MAIL</th>
			<th>AGE</th>
			<th>BIRTHDAY</th>
		</tr>
	{% for item in data %}
		<tr>
			<td>{{item.id}}</td>
			<td>{{item.name}}</td>
			<td>{% if item.gender == False %} male{% endif%}
				{% if item.gender == True %} female{% endif%}</td>
			<td>{{item.mail}}</td>
			<td>{{item.age}}</td>
			<td>{{item.birthday}}</td>
		</tr>
	{% endfor %}
	</table>
</body>

– Friend.objects.get(id=num)でレコードを一つ取り出す
– Managerクラスが取り出している
/hello/views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Friend
from .forms import HelloForm

def index(request):
	params = {
		'title': 'Hello',
		'message': 'all friends.',
		'form': HelloForm(),
		'data': [],
	}
	if(request.method == 'POST'):
		num=request.POST['id']
		item=Friend.objects.get(id=num)
		params['data'] = [item]
		params['form'] = HelloForm(request.POST)
	else:
		params['data'] = Friend.objects.all()
	return render(request, 'hello/index.html', params)

views.pyの関数がMVCのコントローラみたいな役割をしてますね。

[Django]レコードの取得

### レコードの表示
– data = Friend.objects.all() で取り出す
/hello/views.py

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

def index(request):
	data = Friend.objects.all()
	params = {
		'title': 'Hello',
		'message': 'all friends.',
		'data': data,
	}
	return render(request, 'hello/index.html', params)

– {% for item in data %}{% endfor %}で繰り返し処理
– {% if %}{% endif %}で条件分岐
/hello/templates/hello/index.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p class="h5 mt-4">{{message|safe}}</p>
	<table class="table">
		<tr>
			<th>ID</th>
			<th>NAME</th>
			<th>GENDER</th>
			<th>MAIL</th>
			<th>AGE</th>
			<th>BIRTHDAY</th>
		</tr>
	{% for item in data %}
		<tr>
			<td>{{item.id}}</td>
			<td>{{item.name}}</td>
			<td>{% if item.gender == False %} male{% endif%}
				{% if item.gender == True %} female{% endif%}</td>
			<td>{{item.mail}}</td>
			<td>{{item.age}}</td>
			<td>{{item.birthday}}</td>
		</tr>
	{% endfor %}
	</table>
</body>

/hello/urls.py

from django.urls import path
from . import views

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

おおお、いいですね。MVCを触ってる感が出てきました。

[Django]データベースの管理ツール

Djangoにはデータベースの管理ツールが用意されていて、それを使ってWeb上でテーブルなどの編集が行える

### 管理者の作成
$ python manage.py createsuperuser
Username (leave blank to use ‘vagrant’): admin
Email address: ****@gmail.com
Password:
Password (again):
Superuser created successfully.

### モデルを管理ツールで利用できるようにする
– アプリケーションのadmin.pyで編集する
/hello/admin.py

from django.contrib import admin
from .models import Friend

admin.site.register(Friend)

### adminログイン
http://192.168.33.10:8000/admin


– AUTHENTICATION AND AUTHORIZATIONに「Groups」「Users」モデルが用意されている
– 下がmigrationで作成したテーブル

### 管理ツールからレコードの作成

### 利用者の管理ページ
add userから追加する

まあ、便利といえば便利

[Django]データベースの初期設定

Djangoで使えるデータベース: MySQL, PostgreSQL, SQLite
Djangoはプロジェクトにスクリプトを書くと、テーブルを自動生成する

### データベースの設定
– プロジェクトフォルダのsettings.pyを開く

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

– ENGINE: アクセスに使われるプログラム名
– NAME: データベース名 SQLiteの場合、パスを設定する

– mysqlの場合

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'databasename',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

– PostgreSQLの場合

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'databasename',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

### モデルの作成
– アプリケーションのmodels.pyを開く
/hello/models.py

from django.db import models

# Create your models here.

モデルクラスの作成
– 書き方はFormクラスとほぼ同じ
– django.db.modelsにあるModelクラスを継承している
– def __str__(self)はテキストの内容を返すもの

from django.db import models

class Friend(models.Model):
	name = models.CharField(max_length=100)
	mail = models.EmailField(max_length=200)
	gender = models.BooleanField()
	age = models.IntegerField(default=0)
	birthday = models.DateField()

	def __str__(self):
		return '<Friend:id=' + str(self.id) + ', ' + self.name + '(' + self.age + ')>'

### マイグレーションファイルの作成
$ python manage.py makemigrations helloMigrations for ‘hello’:
hello/migrations/0001_initial.py
– Create model Friend

### マイグレーションの実行
$ python manage.py migrate

作成されたmigrantionファイル
/hello/migrations/0001_initial.py

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Friend',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=100)),
                ('mail', models.EmailField(max_length=200)),
                ('gender', models.BooleanField()),
                ('age', models.IntegerField(default=0)),
                ('birthday', models.DateField()),
            ],
        ),
    ]

– Migrationクラスはdjango.db.migraitonsにあるクラス
– operationsが実行するクラス