### 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' %}"> « first</a> </li> <li class="page-item"> <a class="page-link" href="{% url 'message' %}{{data.previous_page_number}}"> « prev</a> </li> {% else %} <li class="page-item"> <a class="page-link"> « first</a> </li> <li class="page-item"> <a class="page-link"> « 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 »</a> </li> <li class="page-item"> <a class="page-link" href="{% url 'message' %}{{data.paginator.num_pages}}"> last »</a> </li> {% else %} <li class="page-item"> <a class="page-link">next »</a> </li> <li class="page-item"> <a class="page-link">last »</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>