[Django]SNSアプリ2

/sns/forms.py

from django import forms
from.models import Message,Group,Friend,Good
from django.contrib.auth.models import User

# Message form
class MessageForm(forms.ModelForm):
	class Meta:
		model = Message
		fields = ['owner', 'group', 'content']

# Group form
class GroupForm(forms.ModelForm):
	class Meta:
		model = Group
		fields = ['owner', 'title']

# Friend form
class FriendForm(forms.ModelForm):
	class Meta:
		model = Friend
		fields = ['owner', 'user', 'group']

# Good form
class GoodForm(forms.ModelForm):
	class Meta:
		model = Good
		fields = ['owner', 'message']

# Group checkbox form
class GroupCheckForm(forms.Form):
	def __init__(self, user, *args, **kwargs):
		super(GroupCheckForm, self).__init__(*args, **kwargs)
		public = User.objects.filter(username='public').first()
		self.fields['groups'] = forms.MultipleChoiceField(
			choices = [(item.title, item.title) for item in Group.objects.filter(owner__in=[user,public])],
		widget = forms.CheckboxSelectMultiple(),
		)

# Group select menu
class GroupSelectForm(forms.Form):
	def __init__(self, user, *args, **kwargs):
		super(GroupSelectForm, self).__init__(*args, **kwargs)
		self.fields['groups'] = forms.ChoiceField(
				choices=[('-','-')] + [(item.title, item.title) for item in Group.objects.filter(owner=user)],widget=forms.Select(attrs={'class':'form-control'}),
			)

# Friend checkbox form
class FriendsForm(forms.Form):
	def __init__(self, user, friends=[], vals=[], *args, **kwargs):
		super(FriendsForm, self).__init__(*args, **kwargs)
		self.fields['friends'] = forms.MultipleChoiceField(
				choices = [(item.user, item.user) for item in friends],
				widget = forms.CheckboxSelectMultiple(),
				initial = vals
			)

# Group making form
class CreateGroupForm(forms.Form):
	group_name = forms.CharField(max_length=50, widget=forms.TextInput(attrs={'class':'form-control'}))

# 投稿フォーム
class PostForm(forms.Form):
	content = forms.CharField(max_length=500, widget=forms.Textarea(attrs={'class':'form-control', 'rows':2}))

	def __init__(self, user, *args, **kwargs):
		super(PostForm, self).__init__(*args, **kwargs)
		public = User.objects.filter(username='public').first()
		self.fields['groups'] = forms.ChoiceField(
				choices=[('-','-')] + [(item.title, item.title) for item in Group.objects. filter(owner__in=[user,public])],
				widget=forms.Select(attrs={'class':'form-control'}), 
		)

/sns/urls.py

from django.urls import path
from . import views

urlpatterns = [
	path('', views.index, name='index'),
	path('<int:page>', views.index, name='index'),
	path('groups', views.groups, name='groups'),
	path('add', views.add, name='add'),
	path('creategroup', views.creategroup, name='creategroup'),
	path('post', views.post, name='post'),
	path('share/<int:share_id>', views.share, name='share'),
	path('good/<int:good_id>', views.good, name='good'),
]

/sns/urls.py

from django.urls import path
from . import views

urlpatterns = [
	path('', views.index, name='index'),
	path('<int:page>', views.index, name='index'),
	path('groups', views.groups, name='groups'),
	path('add', views.add, name='add'),
	path('creategroup', views.creategroup, name='creategroup'),
	path('post', views.post, name='post'),
	path('share/<int:share_id>', views.share, name='share'),
	path('good/<int:good_id>', views.good, name='good'),
]

/django_app/urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', include('hello.urls')),
    path('sns/', include('sns,urls')),
]

/sns/views.py

from django.shortcuts import render
from django.shortcuts import redirect
from django.contrib.auth.models import User
from django.contrib import messages
from django.core.paginator import Paginator
from django.db.models import Q
from django.contrib.auth.decorators import login_required

from .models import Message,Friend,Group,Good
from .forms import GroupCheckForm, GroupSelectForm, FriendsForm, CreateGroupForm, PostForm

# index view
@login_required(login_url='/admin/login/')
def index(request, page=1):
	# publicのuserを取得
	(public_user, public_group) = get_public()

	# post送信時の処理
	if request.method == 'POST':

		# Groupsのチェックを更新した時の処理
		# フォームを用意
		checkform = GroupCheckForm(request.user, request.POST)
		# チェックされたGroup名をリストにまとめる
		glist = []
		for item in request.POST.getlist('groups'):
			glist.append(item)
		# Messageの取得
		messages = get_your_group_message(request.user, glist, page)

	# GETアクセス時の処理
	else:
		# フォームの用意
		checkform = GroupCheckForm(request.user)
		# Groupのリスト取得
		gps = Group.objects.filter(owner=request.user)
		glist = [public_group.title]
		for item in gps:
			glist.append(item.title)
		# メッセージの取得
		messages = get_your_group_message(request.user, glist, page)

	# 共通処理
	params = {
		'login_user': request.user,
		'content': messages,
		'check_form': checkform,
	}
	return render(request, 'sns/index.html', params)

@login_required(login_url='/admin/login/')
def groups(request):
	# 自分が登録したFriendを取得
	friends = Friend.objects.filter(owner=request.user)

	# POST送信時の処理
	if request.method == 'POST':

		# Groupsメニュー選択肢の処理
		if request.POST['mode'] == '__groups_form__':
			# 選択したGroup名を取得
			sel_group = request.POST['groups']
			# Groupを取得
			gp = Group.objects.filter(owner=request.user).filter(title=sel_group).first()
			# Groupに含まれるFriend取得
			fds = Friend.objects.filter(owner=request.user).filter(group=gp)
			print(Friend.objects.filter(owner=request.user))
			# FriendのUserをリストにまとめる
			vlist = []
			for item in fds:
				vlist.append(item.user.username)
			# フォームの用意
			groupsform = GroupSelectForm(request.user,request.POST)
			friendsform = FriendsForm(request.user, friends=friends, vals=vlist)

		# Friendsのチェック更新時の処理
		if request.POST['mode'] == '__friends_form__':
			# 選択したGroupの取得
			sel_group = request.POST['group']
			group_obj = Group.objects.filter(title=sel_group).first()
			print(group_obj)
			# チェックしたFriendsを取得
			sel_fds = request.POST.getlist('friends')
			# FriendsのUserを取得
			sel_users = User.objects.filter(username__in=sel_fds)
			# Userのリストに含まれるユーザが登録したFriendを取得
			fds = Friend.objects.filter(owner=request.user).filter(user__in=sel_users)
			# 全てのFriendにGroupを設定し保存する
			vlist = []
			for item in fds:
				item.group = group_obj
				item.save()
				vlist.append(item.user.username)
			# メッセージを設定
			messages.success(request, ' チェックされたFriendを' + sel_group + 'に登録しました')
			# フォームの用意
			groupsform = GroupSelectForm(request.user, {'groups':sel_group})
			friendsform = FriendsForm(request.user, friends=friends, vals=vlist)

	# GETアクセス時の処理
	else:
		# フォームの用意
		groupsform = GroupSelectForm(request.user)
		friendsform = FriendsForm(request.user, friends=friends, vals=[])
		sel_group = '-'

	# 共通処理
	createform = CreateGroupForm()
	params = {
		'login_user': request_user,
		'groups_form': groupsform,
		'friends_form': friendsform,
		'create_form': createform,
		'group': sel_group,
	}
	return render(request, 'sns/groups.html', params)

# Friendの追加処理
@login_required(login_url='/admin/login/')
def add(request):
	# 追加するUserを取得
	add_name = request.GET['name']
	add_user = User.objects.filter(username=add_name).first()
	# Userが本人だった場合の処理
	if add_user == request.user:
		messages.info(request, "自分自身をFriendに追加することはできません。")
		return redirect(to='/sns')
	# publicの取得
	(public_user, public_group) = get_public()
	# add_userのFriendの数を調べる
	frd_num = Friend.objects.filter(owner=request.user).filter(user=add_user).count()
	# ゼロより大きければ既に登録済み
	if frd_num > 0:
		messages.info(request, add_user.username + ' は既に追加されています。')
		return redirect(to='/sns')

	# ここからFriendの登録処理
	frd = Friend()
	frd.owner = request.user
	frd.user = add_user
	frd.group = public_group
	frd.save()
	# メッセージを設定
	messages.success(request, add_user.username + ' を追加しました! groupページに移動して、追加したFriendをメンバーに設定してください。')
	return redirect(to='/sns')

# グループの作成処理
@login_required()
def creategroup(request):
	# Groupを作りUserとtitleを設定して保存する
	gp = Group()
	gp.owner = request.user
	gp.title = request.user.username + 'の' + request.POST['group_name']
	gp.save()
	messages.info(request, '新しいグループを作成しました。')
	return redirect(to='/sns/groups')

# メッセージのポスト処理
@login_required(login_url='/admin/login/')
def post(request):
	# POST送信の処理
	if request.method == 'POST':
		# 送信内容の取得
		gr_name = request.POST['groups']
		content = request.POST['content']
		# Groupの取得
		group = Group.objects.filter(owner=request.user).filter(title=gr_name).first()
		if group == None:
			(pub_user, group) = get_public()
		# Messageを作成し設定して保存
		msg = Message()
		msg.owner = request.user
		msg.group = group
		msg.content = content
		msg.save()
		# メッセージを設定
		messages.success(request, '新しいメッセージを投稿しました!')
		return redirect(to='sns')

	# GETアクセス時の処理
	else:
		form = PostForm(request.user)

	# 共通処理
	params = {
		'login_user': request.user,
		'form': form, 
	}
	return render(request, 'sns/post.html', params)

# 投稿をシェアする
@login_required(login_url='/admin/login/')
def share(request, share_id):
	# シェアするMessageの取得
	share = Message.objects.get(id=share_id)
	print(share)
	# POST送信時の処理
	if request.method == 'POST':
		# 送信内容を取得
		gr_name = request.POST['groups']
		content = request.POST['content']
		# Groupの取得
		group = Group.objects.filter(owner=request.user).filter(title=gr_name).first()
		if group == None:
			(pub_user, group) = get_public()
		# メッセージを作成し、設定をして保存
		msg = Message()
		msg.owner = request.user
		msg.group = group
		msg.content = content
		msg.share_id = share.id
		msg.save()
		share_msg = msg.get_share()
		share_msg.share_content += 1
		share_msg.save()
		# メッセージを設定
		messages.sucess(request, 'メッセージをシェアしました!')
		return redirect(to='/sns')

	# 共通処理
	form = PostForm(request.user)
	params = {
		'login_user': request.user,
		'form': form,
		'share': share,
	}
	return render(request, 'sns/share.html', params)

# goodボタンの処理
@login_required(login_url='/admin/login')
def good(request, good_id):
	# goodするMessageを取得
	good_msg = Message.objects.get(id=good_id)
	# 自分がメッセージにGoodした数を調べる
	is_good = Good.objects.filter(owner=request.user).filter(message=good_msg).count()
	# ゼロより大きければ既にgood済み
	if is_good > 0:
		messages.success(request, '既にメッセージにはGoodしています。')
		return redirect(to='/sns')

	# Messageのgood_countを1増やす
	good_msg.good_count += 1
	good_msg.save()
	# Goodを作成し、設定して保存
	good = Good()
	good.owner = request.user
	good.message = good_msg
	good.save()
	# メッセージを設定
	messages.success(request, 'メッセージにGoodしました!')
	return redirect(to='/sns')

# 指定されたグループおよび検索文字によるMessageの取得
def get_your_group_message(owner, glist, page):
	page_num = 10 # ページあたりの表示数
	# publicの取得
	(public_user, public_group) = get_public()
	# チェックされたGroupの取得
	groups = Group.objects.filter(Q(owner=owner)|Q(owner=public_user)).filter(title__in_groups)
	# FriendのUserをリストにまとめる
	me_users = []
	for f in me_friends:
		me_users.append(f.user)
	# UserリストのUserが作ったGroupの取得
	his_groups = Group.objects.filter(owner__in=me_users)
	his_friends = Friend.objects.filter(user=owner).filter(group__in=his_groups)
	me_groups = []
	for hf in his_friends:
		me_groups.append(hf.group)
	# groupがgroupに含まれるか、me_groupsに含まれるMessageの取得
	messages = Message.objects.filter(Q(group__in=groups)|Q(group__in=me_groups))
	# ページネーションで指定ページを取得
	page_item = Paginator(messages, page_num)
	return page_item.get_page(page)

# publicなUserとGroupを取得する
def get_public():
	public_user = User.objects.filter(username='public').first()
	public_group = Group.objects.filter(owner=public_user).first()
	return (public_user, public_group)

– @login_required(login_url=’/admin/login’)でログインしないとアクセスできなくなる
– from django.contrib.auth.decorators import Userが必須

### テンプレートの作成
/sns/templates/sns/layout.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>{% block title %}{% endblock %}</title>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" crossoorigin="anonymous">
</head>
<body>
	<nav class="navbar navbar-expand navbar-light bg-light">
	<ul class="navbar-nav mr-auto">
		<li class="nav-item">
			<a class="nav-link" href="{% url 'index' %}">top</a>
		</li>
		<li class="nav-item">
			<a class="nav-link" href="{% url 'post' %}">post</a>
		</li>
		<li class="nav-item">
			<a class="nav-link" href="{% url 'groups' %}">group</a>
		</li>
	</ul>
	<span>logined: <span class="h6">"{{login_user}}".</span></span>
	</nav>

	<div>{% block header %}{% endblock %}</div>
	<div class="content">{% block content %}{% endblock %}</div>

	<hr>
	<div class="my-3">
		<span class="font-weight-bold">
			<a href="/admin/logout?next=/sns/">[ logout ]</a>
		</span>
	</div>	
</body>
</html>

– {% block ○○ %}{% endblock %}にコンテンツを用意する

/sns/templates/sns/index.html

{% extends 'sns/layout.html' %}

{% block title %}Index{% endblock %}

{% block header %}
<script>
function sendGroupForm(page){
	document.group_form.action += page;
	document.group_form.submit();
}
</script>
<h1 class="display-4 text-primary">SNS</h1>
<p>※グループのチェックをONにしてupdateすると、そのグループに登録されている利用者のメッセージだけが表示されます。</p>
{% if messages %}
<ul class="messages">
	{% for message in messages %}
	<li {% if message.tags %} class="{{ message.tags }}" {% endif %}></li>
	{% endfor %}
</ul>
{% endif %}
{% endblock %}

{% block content %}
<hr>
<div>
	<form action="{% url 'index' %}" method="post" name="group_form">
		{% csrf_token %}
		{{ check_form }}
		<div>
			<button class="btn btn-primary">update</button>
		</div>
	</form>
</div>
<table class="table mt-3">
	<tr><th>Messages</th></tr>
	{% for item in contents %}
	<tr><td>
		<p class="my-0">
			{% if item.group.title == 'public' %}
			<span class="bg-info text-light px-1">Public</span>
			{% endif %}
		{{item.content}}</p>
		<p class="">({{item.pub_date}})</p>
		{% if item.share_id > 0 %}
		<ul><li class="text-black-50">"{{item.get_share}}"</li></ul>
		{% endif %}
		<span class="float-left text-info"> share="{{item.share_count}} good={{item.good_count}}</span>
		<span class="float-right">
			"{{item.owner}}"(<a href="{% url 'add' %}?name=({item.owner})">add friend</a>)
			<a href="{% url 'share' item.id %}"><button class="py-0">share</button></a>
			<a href="{% url 'good' item.id %}"><button class="py-0">good!</button></a>
		</span>
	</td></tr>
	{% endfor %}
</table>

<ul class="pagination justify-content-center">
	{% if contents.has_previous %}
		<li class="page-item">
			<a class="page-link" href="javascript:sendGroupForm(1);">
			&laquo; first</a>
		</li>
		<li class="page-item">
			<a class="page-link" href="javascript:sendGroupForm({{contents.previous_page_number}});">
			&laquo; prev</a>
		</li>
		{% else %}
		<li class="page-item">
			<a class="page-link">
			&laquo; first</a>
		</li>
		<li class="page-item">
			<a class="page-link">
			&laquo; prev</a>
		</li>
		{% endif %}
		<li class="page-item">
			<a class="page-link">
			{{contents.number}}/{{contents.paginator.num_pages}}</a>
		</li>
		{% if data.has_next %}
		<li class="page-item">
			<a class="page-link" href="javascript:sendGroupForm({{contents.next_page_number}});">
			next &raquo;</a>
		</li>
		<li class="page-item">
			<a class="page-link" href="javascript:sendGroupForm({{contents.paginator.num_pages}})">
			last &raquo;</a>
		</li>
		{% else %}
		<li class="page-item">
			<a class="page-link">next &raquo;</a>
		</li>
		<li class="page-item">
			<a class="page-link">last &raquo;</a>
		</li>
		{% endif %}
	</ul>
{% endblock %}

/sns/templates/sns/post.html

{% extends 'sns/layout.html' %}

{% block title %}Post{% endblock %}

{% block header %}
<h1 class="display-4 text-primary">Post</h1>
<p class="caption">※投稿先のグループを選択し、メッセージを投稿します。</p>
{% if messages %}
<ul class="list-group">
	{% for message in messages %}
	<li {% if message.tags %} class="list-group-item {{ message.tags }}" {% endif %}>{{ message }}</li>
	{% endfor %}
</ul>
{% endif %}
{% endblock %}

{% block content %}
<form action="{% url 'post' %}" method="post">
	{% csrf_token %}
	{{form.as_p}}
	<button class="btn btn-primary">Post!</button>
</form>
{% endblock %}

/sns/templates/sns/share.html

{% extends 'sns/layout.html' %}

{% block title %}Share{% endblock %}

{% block header %}
<h1 class="display-4 text-primary">Share</h1>
<p class="caption">※投稿先のグループを選択し、下のメッセージをシェアします。</p>
{% if messages %}
<ul class="messages">
	{% for message in messages %}
	<li{% if message tags %} class="{{ message.tags }}" {% endif %}>{{ message }}</li>
	{% endfor %}
</ul>
{% endif %}
{% endblock %}

{% block content %}
<p class="bg-light p-3">
"{{share.content}}({{share.owner}})"</p>
<form action="{% url 'share' share.id %}" method="post">
	{% csrf_token %}
	{{form.as_p}}
	<button class="btn btn-primary mt-2">Share!</button>
</form>
{% endblock %}

/sns/templates/sns/groups.html

{% extends 'sns/layout.html' %}

{% block title %}Groups{% endblock %}

{% block header %}
<h1 class="display-4 text-primary">Group</h1>
<p class="caption">※グループを選択してselect memberすると、
	そのグループに登録されている利用者がONになります。
利用者のチェックをONにしてset memberすると、ONにしてある利用者がグループに追加されます。</p>
{% if messages %}
<ul class="messages">
	{% for message in messages %}
	<li{% if message.tags %} class="{{ message.tags }}" {% endif %}>{{message}}</li>
	{% endfor %}
</li>
{% endif %}
{% endblock %}

{% block content %}
<form action="{% url 'groups' %}" method="post">
	{% csrf_token %}
	<input type="hidden" name="mode" value="__groups_form__">
	{{groups_form}}
	<button class="btn btn-primary mt-1">select members</button>
</form>
<hr>
<form action="{% url 'groups' %}" method="post">
	{% csrf_token %}
	<input type="hidden" name="mode" value="__friends_form__">
	<input type="hidden" name="group" value="{{group}}">
	{{friends_form}}
	<button class="btn btn-primary mt-0">set members</button>
</form>
<hr>
<p>※新しいGroupの登録</p>
<form action="{% url 'creategroup' %}" method="post">
	{% csrf_token %}
	{{create_form}}
	<button class="btn btn-primary mt-1">create new group</button>
</form>
{% endblock %}

[Django]SNSアプリ1

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

– settingsにアプリケーション名を追加
/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',
    'sns',
]

### アプリケーションの設計
– 機能の洗い出し
– データベース設計
– 各ページの設計

### モデルの作成
– テーブルはUser, Message, Group, Friend, Good
– Message: owner(投稿者), group, content, share_id, good_count, share_count, pub_date
– Group: owner, title
– Friend: owner, user, group
– Good: owner, message
/sns/models.py

from django.db import models
from django.contrib.auth.models import User

# Message
class Message(models.Model):
	owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='message_owner')
	group = models.ForeignKey('Group', on_delete=models.CASCADE)
	content = models.TextField(max_length=1000)
	share_id = models.IntegerField(default=-1)
	good_count = models.IntegerField(default=0)
	pub_date = models.DateTimeField(auto_now_add=True)

	def __str__(self):
		return str(self.content) + ' (' + str(self.owner) + ')'

	class Meta:
		ordering = ('-pub_date',)

# Group
class Group(models.Model):
	owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='group_owner')
	title = models.CharField(max_length=100)

	def __str__(self):
		return '<' + self.title + '(' + str(self.owner) + ')>'

# Friend
class Friend(models.Model):
	owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='friend_owner')
	user = models.ForeignKey(User, on_delete=models.CASCADE)
	group = models.ForeignKey(Group, on_delete=models.CASCADE)

	def __str__(self):
		return str(self.user) + ' (group:"' + str(self.group) + '")'

# Good
class Good(models.Model):
	owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='good_owner')
	message = models.ForeignKey(Message, on_delete=models.CASCADE)

	def __str__(self):
		return 'good for "' + str(self.message) + '" (by' + str(self.owner) + ')'

– -pub_dateはpub_dateの大きい順

### マイグレーション
$ python manage.py makemigrations sns
$ python manage.py migrate

### admin.py
/sns/admin.py

from django.contrib import admin
from .models import Message, Friend, Group, Good

admin.site.register(Message)
admin.site.register(Friend)
admin.site.register(Group)
admin.site.register(Good)

$ python manage.py runserver 192.168.33.10:8000

– AddUserからpublicユーザの作成
– ユーザ作成
– publicグループを用意

[Django]メッセージの投稿

### Messageモデル
/hello/models.py

class Message(models.Model):
	friend = models.ForeignKey(Friend, on_delete=models.CASCADE)
	title = models.CharField(max_length=100)
	content = models.CharField(max_length=300)
	pub_date = models.DateTimeField(auto_now_add=True)

	def __str__(self):
		return '<Message:id=' + str(self.id) + ', ' + self.title + '(' + str(self.pub_date) + ')>'

	class Meta:
		ordering = ('pub_date',)

– on_delete=models.CASCADEは削除を指定するという意味
– auto_now_add=Trueは自動的に値を設定
– Metaはクラスの基本的設定を記述

### マイグレーション実行
$ python manage.py makemigrations helloMigrations for ‘hello’:
hello/migrations/0002_auto_20200712_2230.py
– Alter field age on friend
– Alter field name on friend
– Create model Message
$ python manage.py migrate

### admin.py
/hello/admin.py

from django.contrib import admin
from .models import Friend, Message

admin.site.register(Friend)
admin.site.register(Message)

http://192.168.33.10:8000/admin/

### Messageページ作成
/hello/urls.py

	path('message/', views.message, name='message'),
	path('message/<int:page>', views.message, name='message'),

/hello/forms.py

from.models import Friend, Message

class MessageForm(forms.ModelForm):
	class Meta:
		model = Message
		fields = ['title', 'content', 'friend']
		widgets = {
			'title': forms.TextInput(attrs={'class':'formcontrol form-control-sm'}),
			'content': forms.Textarea(attrs={'class':'form-control form-control-sm', 'rows':2}),
			'friend': forms.Select(attrs={'class':'form-control form-control-sm'}),
		}

/hello/views.py

from .models import Friend, Message
from .forms import FriendForm, MessageForm

def message(request, page=1):
	if(request.method == 'POST'):
		obj = Message()
		form = MessageForm(request.POST, instance=obj)
		form.save()
	data = Message.objects.all().reverse()
	paginator = Paginator(data, 5)
	params = {
		'title': 'Message',
		'form': MessageForm(),
		'data': paginator.get_page(page),
	}
	return render(request, 'hello/message.html', params)

/hello/templates/hello/message.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<form action="{% url 'message' %}" method="post">
		{% csrf_token %}
		{{ form.as_p }}
		<input type="submit" value="send" class="btn btn-primary">
		<div class="mt-5"></div>
		<table class="table">
			<tr>
				<th class="py-1">title</th>
				<th class="py-1">name</th>
				<th class="py-1">datetime</th>
			</tr>
		{% for item in data %}
			<tr>
				<td class="py-2">{{item.title}}</td>
				<td class="py-2">{{item.friend.name}}</td>
				<td class="py-2">{{item.pub_date}}</td>
			</tr>
		{% endfor %}
		</table>

	<ul class="pagination justify-content-center">
		{% if data.has_previous %}
		<li class="page-item">
			<a class="page-link" href="{% url 'message' %}">
			&laquo; first</a>
		</li>
		<li class="page-item">
			<a class="page-link" href="{% url 'message' %}{{data.previous_page_number}}">
			&laquo; prev</a>
		</li>
		{% else %}
		<li class="page-item">
			<a class="page-link">
			&laquo; first</a>
		</li>
		<li class="page-item">
			<a class="page-link">
			&laquo; prev</a>
		</li>
		{% endif %}
		<li class="page-item">
			<a class="page-link">
			{{data.number}}/{{data.paginator.num_pages}}</a>
		</li>
		{% if data.has_next %}
		<li class="page-item">
			<a class="page-link" href="{% url 'message' %}{{data.next_page_number}}">
			next &raquo;</a>
		</li>
		<li class="page-item">
			<a class="page-link" href="{% url 'message' %}{{data.paginator.num_pages}}">
			last &raquo;</a>
		</li>
		{% else %}
		<li class="page-item">
			<a class="page-link">next &raquo;</a>
		</li>
		<li class="page-item">
			<a class="page-link">last &raquo;</a>
		</li>
		{% endif %}
	</ul>
</body>

/hello/templates/hello/index.html

<table class="table">
			<tr>
				<th>id</th>
				<th>name</th>
				<th>age</th>
				<th>mail</th>
				<th>birthday</th>
				<th>Messages</th>
			</tr>
		{% for item in data %}
			<tr>
				<td>{{item.id}}</td>
				<td>{{item.name}}</td>
				<td>{{item.age}}</td>
				<td>{{item.mail}}</td>
				<td>{{item.birthday}}</td>
				<td><ul>
					{% for ob in item.message_set.all %}
						<li>{{ob.title}}</li>
					{% endfor %}
				</ul>
				</td>
			</tr>
		{% endfor %}
		</table>

[Django]テーブルのリレーション

### OneToManyの関連付け
主モデル
class A(models.Model):

従モデル
class B(models.Model)
項目 = models.ForeignKey(model name)
※ForeignKeyは外部クラスのキー

### OneToOneの関連付け
主モデル
class A(models.Model):

従モデル
class B(models.Model)
項目 = models.OneToOneField(model name)

### ManyToManyの関連付け
主モデル
class A(models.Model):

従モデル
class B(models.Model)
項目 = models.ManyToManyField(model name)

なるほど、MySQLのForeignKeyでindexを貼るって、レコードのリレーションの事だったのか。点と点が繋がるタイミングは感動する。

[Django]ページネーション

Djangoでは「Paginator」というクラスが用意されている
– 変数 = Paginator(コレクション、レコード数)
– 指定ページのレコードを取り出すには「get_page」メソッドを使う

### Paginatorを使用する
/hello/urls.py

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

※ path(”, views.index, name=’index’)は削除しない

/hello/views.py

from django.core.paginator import Paginator

def index(request, num=1):
	data = Friend.objects.all()
	page = Paginator(data, 3)
	params = {
		'title': 'Hello',
		'message': '',
		'data': page.get_page(num),
	}		
	return render(request, 'hello/index.html', params)

Paginator(data, 3)でQuerySetオブジェクトからPaginatorインスタンスを作成する

/hello/templates/hello/index.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<table class="table">
		<tr>
			<th>id</th>
			<th>name</th>
			<th>age</th>
			<th>mail</th>
			<th>birth</th>
		</tr>
	{% for item in data %}
		<tr>
			<td>{{item.id}}</td>
			<td>{{item.name}}</td>
			<td>{{item.age}}</td>
			<td>{{item.mail}}</td>
			<td>{{item.birthday}}</td>
		</tr>
	{% endfor %}
	</table>
	<ul class="pagination">
		{% if data.has_previous %}
		<li class="page-item">
			<a class="page-link" href="{% url 'index' %}">
			&laquo; first</a>
		</li>
		<li class="page-item">
			<a class="page-link" href="{% url 'index' %}{{data.previous_page_number}}">
			&laquo; prev</a>
		</li>
		{% else %}
		<li class="page-item">
			<a class="page-link">
			&laquo; first</a>
		</li>
		<li class="page-item">
			<a class="page-link">
			&laquo; prev</a>
		</li>
		{% endif %}
		<li class="page-item">
			<a class="page-link">
			{{data.number}}/{{data.paginator.num_pages}}</a>
		</li>
		{% if data.has_next %}
		<li class="page-item">
			<a class="page-link" href="{% url 'index' %}{{data.next_page_number}}">
			next &raquo;</a>
		</li>
		<li class="page-item">
			<a class="page-link" href="{% url 'index' %}{{data.paginator.num_pages}}">
			last &raquo;</a>
		</li>
		{% else %}
		<li class="page-item">
			<a class="page-link">next &raquo;</a>
		</li>
		<li class="page-item">
			<a class="page-link">last &raquo;</a>
		</li>
		{% endif %}
	</ul>
</body>

これは凄い

[Django]バリデータ関数の作成とエラーメッセージ

### バリデータ作成
– reがpythonの正規表現モジュール
/hello/models.py

import re
from django.db import models
from django.core.validators import ValidationError

def number_only(value):
	if(re.match(r'^[0-9]*$', value) == None):
		raise ValidationError(
				'%(value)s is not Number!', params={'value': value},
			)

class Friend(models.Model):
	name = models.CharField(max_length=100, validators=[number_only])
	mail = models.EmailField(max_length=200)
	gender = models.BooleanField()
	age = models.IntegerField()
	birthday = models.DateField()

### エラーメッセージ作成
– フォーム全体のエラーメッセージを取り出すにはform.errorsを使う
– フォームの項目からエラーを取り出すことができる
/hello/templates/hello/check.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<ol class="list-group">
		{% for item in form %}
		<li class="list-group-item py-2">{{ item.name }}{{ item.value }}:{{ item.errors.as_text }}</li>
		{% endfor %}
	</ol>
	<table class="table mt-4">
		<form action="{% url 'check' %}" method="post">
			{% csrf_token %}
			<tr>
				<th>名前</th>
				<td>{{ form.name }}</td>
			</tr>
			<tr>
				<th>メール</th>
				<td>{{ form.mail }}</td>
			</tr>
			<tr>
				<th>性別</th>
				<td>{{ form.gender }}</td>
			</tr>
			<tr>
				<th>年齢</th>
				<td>{{ form.age }}</td>
			</tr>
			<tr>
				<th>誕生日</th>
				<td>{{ form.birthday }}</td>
			</tr>
			<tr><th></th><td>
			<input type="submit" value="click" class="btn btn-primary">
			</td></tr>
		</form>
	</table>
</body>

### ModelFormのカスタマイズ
/hello/forms.py

class FriendForm(forms.ModelForm):
	class Meta:
		model = Friend
		fields = ['name','mail','gender','age','birthday']
		widgets = {
			'name': forms.TextInput(attrs={'class':'form-control'}),
			'mail': forms.EmailInput(attrs={'class':'form-control'}),
			'age': forms.NumberInput(attrs={'class':'form-control'}),
			'birthday': forms.DateInput(attrs={'class':'form-control'}),
		}

/hello/templates/hello/check.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<ol class="list-group mb-4">
		{% for item in form %}
		<li class="list-group-item py-2">{{ item.name }}{{ item.value }}:{{ item.errors.as_text }}</li>
		{% endfor %}
	</ol>
		<form action="{% url 'check' %}" method="post">
			{% csrf_token %}
			<div class="form-group">名前{{ form.name }}</div>
			<div class="form-group">メール{{ form.mail }}</div>
			<div class="form-group">性別{{ form.gender }}</div>
			<div class="form-group">年齢{{ form.age }}</div>
			<div class="form-group">誕生日{{ form.birthday }}</div>
			<div class="form-group">
			<input type="submit" value="click" class="btn btn-primary">
			</div>
</body>

[Django]ModelFormのバリデーション

– バリデーションはsaveの時に実行されるので、モデルの更新時にはモデル側でバリデーションをかける

/hello/forms.py

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

/hello/models.py

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()

– save以外に自分でチェックをかけることも可能
/hello/views.py

def check(request):
	params = {
		'title': 'Hello',
		'message': 'check validation.',
		'form': FriendForm(),
	}
	if(request.method == 'POST'):
		obj = Friend()
		form = FriendForm(request.POST, instance=obj)
		params['form'] = form
		if(form.is_valid()):
			params['message'] = 'OK!'
		else:
			params['message'] = 'no good.'
	return render(request, 'hello/check.html', params)

/hello/templates/hello/check.html

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

– forms.FormとModelFormのバリデーションは異なる

### モデルにバリデーションの組み込み
/hello/models.py

from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator

class Friend(models.Model):
	name = models.CharField(max_length=100)
	mail = models.EmailField(max_length=200)
	gender = models.BooleanField()
	age = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(150)])
	birthday = models.DateField()

### モデルで使えるバリデータ
– MinValueValidator/MaxValueValidator, MinLengthValidator/MaxLengthValidator

from django.db import models
from django.core.validators import MinLengthValidator

class Friend(models.Model):
	name = models.CharField(max_length=100, validators=[MinLengthValidator(10)])
	mail = models.EmailField(max_length=200, validators=[MinLengthValidator(10)])
	gender = models.BooleanField()
	age = models.IntegerField()
	birthday = models.DateField()

– EmailValidator/URLValidator
– ProhibitNullCharactersValidator
– RegexValidator

from django.core.validators import RegexValidator

class Friend(models.Model):
	name = models.CharField(max_length=100, validators=[RegexValidator(r'^[a-z]*$')])

バリデーション定義によってmodelに組み込んでいくイメージです。

[Django]バリデーションの種類

### CharFieldのバリデーション
– required
– min_length, max_length
– empty_value

class CheckForm(forms.Form):
	empty = forms.CharField(label='Empty', empty_value=True, widget=forms.TextInput(attrs={'class':'form-control'}))
	min = forms.CharField(label='Min', min_length=10, widget=forms.TextInput(attrs={'class':'form-control'}))	
	max = forms.CharField(label='Max', max_length=10, widget=forms.TextInput(attrs={'class':'form-control'}))

### IntegerField/FloatFieldのバリデーション
– required
– min_value, max_value

class CheckForm(forms.Form):
	required = forms.IntegerField(label='Required', widget=forms.NumberInput(attrs={'class':'form-control'}))
	min = forms.IntegerField(label='Min', min_value=100, widget=forms.NumberInput(attrs={'class':'form-control'}))	
	max = forms.IntegerField(label='Max', max_value=1000, widget=forms.NumberInput(attrs={'class':'form-control'}))

### 日時関連のバリデーション
– DateField, TimeField, DateTimeFieldでは日時の各値を表す記号を組み合わせて作成する
– %y, %m, %d, %H, %M, %S

class CheckForm(forms.Form):
	date = forms.DateField(label='Date', input_formats=['%d'], widget=forms.DateInput(attrs={'class':'form-control'}))
	time = forms.TimeField(label='Time', widget=forms.TimeInput(attrs={'class':'form-control'}))
	datetime = forms.DateTimeField(label='DateTime', widget=forms.DateTimeInput(attrs={'class':'form-control'}))

### バリデーションを追加
– Formクラスにメソッドを追加する
– raise ValidationError(“Error message”)
/hello/forms.py

from django import forms

class CheckForm(forms.Form):
	str = forms.CharField(label='String', widget=forms.TextInput(attrs={'class':'form-control'}))

	def clean(self):
		cleaned_data = super().clean()
		str = cleaned_data['str']
		if(str.lower().startswith('no')):
			raise forms.ValidationError('Your input "No!"')

[Django]バリデーション基礎

if(<

>.is_valid()):
# エラー時の処理
else:
# 正常時の処理

### バリデーションを使ってみる
/hello/templates/hello/check.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<form action="{% url 'check' %}" method="post">
		{% csrf_token %}
		{{ form.as_table }}
		<input type="submit" value="click" class="btn btn-primary mt-2">
	</form>
</body>

/hello/urls.py

	path('check', views.check, name='check')

/hello/forms.py

class CheckForm(forms.Form):
	str = forms.CharField(label='Name', widget=forms.TextInput(attrs={'class':'form-control'}))	

/hello/views.py

from .forms import CheckForm

def check(request):
	params = {
		'title': 'Hello',
		'message': 'check validation.',
		'form': CheckForm(),
	}
	if(request.method == 'POST'):
		form = CheckForm(request.POST)
		param['form'] = form
		if(form.is_valid()):
			params['message'] = 'OK!'
		else:
			params['message'] = 'no good.'
	return render(request, 'hello/check.html', params)

スペルミスでmethod=”post”がmetho=”post”になってた。道理で動かない訳だ。

[Django]SQLを直接実行

Managerクラスに用意されている「raw」メソッドを使う

/hello/views.py

def find(request):
	if(request.method == 'POST'):
		msg = request.POST['find']
		form = FindForm(request.POST)
		sql = 'select * from hello_friend'
		if(msg != ''):
			sql += ' where ' + msg
		data = Friend.objects.raw(sql)
		msg = sql
	else:
		msg = 'search words...'
		form = FindForm()
		data =Friend.objects.all()
	params = {
		'title': 'Hello',
		'message': msg,
		'form': form,
		'data': data,
	}
	return render(request, 'hello/find.html', params)

テーブル名はhello_friend
マイグレーションの際に「アプリケーション名_モデル名」で作成される

まあ、SQLの方が慣れているといえば慣れてます。