質問編集履歴

4

文法の修正

2019/11/18 15:53

投稿

pi-natu
pi-natu

スコア5

test CHANGED
File without changes
test CHANGED
@@ -68,27 +68,23 @@
68
68
 
69
69
 
70
70
 
71
- #blog/forms.py
71
+ #blog/urls.py
72
72
 
73
73
  ```python
74
74
 
75
- from django import forms
75
+ from django.urls import path
76
+
77
+ from . import views
76
78
 
77
79
 
78
80
 
79
- from .models import Post
81
+ urlpatterns = [
80
82
 
83
+ path('', views.post_list, name='post_list'),
81
84
 
85
+ path('post/<int:pk>/post_form/', views.post_form, name='post_form'),
82
86
 
83
- class PostForm(forms.ModelForm):
84
-
85
-
86
-
87
- class Meta:
87
+ ]
88
-
89
- model = Post
90
-
91
- fields = '__all__'
92
88
 
93
89
 
94
90
 
@@ -104,7 +100,7 @@
104
100
 
105
101
  {% for post in posts %}
106
102
 
107
- <form action="" method="POST">
103
+ <form action="{% url 'post_form' pk=post.pk %}" method="POST">
108
104
 
109
105
  {% csrf_token %}
110
106
 
@@ -156,15 +152,15 @@
156
152
 
157
153
  if request.method == 'POST':
158
154
 
159
- form = PostForm(request.POST)
155
+ post = get_object_or_404(Post, pk=pk)
160
156
 
161
- if form.is_valid():
157
+ post.up_number += 1
162
158
 
163
- up_number = int(form.cleaned_data['up_number']) + 1
159
+ post.save()
164
160
 
165
- form.save()
166
161
 
162
+
167
- return redirect('/')
163
+ return render(request, 'blog/post_list')
168
164
 
169
165
 
170
166
 

3

文法の修正

2019/11/18 15:53

投稿

pi-natu
pi-natu

スコア5

test CHANGED
File without changes
test CHANGED
@@ -84,7 +84,13 @@
84
84
 
85
85
 
86
86
 
87
+ class Meta:
88
+
89
+ model = Post
90
+
87
- up_number = forms.IntegerField()
91
+ fields = '__all__'
92
+
93
+
88
94
 
89
95
 
90
96
 
@@ -150,13 +156,17 @@
150
156
 
151
157
  if request.method == 'POST':
152
158
 
159
+ form = PostForm(request.POST)
160
+
161
+ if form.is_valid():
162
+
163
+ up_number = int(form.cleaned_data['up_number']) + 1
164
+
165
+ form.save()
166
+
167
+ return redirect('/')
153
168
 
154
169
 
155
- post.up_number = int(post.cleaned_data['up_number']) + 1
156
-
157
-
158
-
159
- post.save()
160
170
 
161
171
 
162
172
 

2

2019/11/15 07:27

投稿

pi-natu
pi-natu

スコア5

test CHANGED
File without changes
test CHANGED
File without changes

1

書式の改善

2019/11/14 09:03

投稿

pi-natu
pi-natu

スコア5

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,4 @@
1
1
  ### 前提・実現したいこと
2
-
3
- ※ここに写真を載せていない分はDjango Girlsのチュートリアルと同じです。(ディレクトリも同様)
4
2
 
5
3
  Djangoでデータベースの更新を行いたいです。
6
4
 
@@ -22,9 +20,47 @@
22
20
 
23
21
  ### 該当のソースコード
24
22
 
25
- #models.py
23
+ #blog/models.py
26
24
 
25
+ ```python
26
+
27
+ from django.conf import settings
28
+
29
+ from django.db import models
30
+
31
+ from django.utils import timezone
32
+
33
+
34
+
35
+
36
+
37
+ class Post(models.Model):
38
+
39
+ author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
40
+
27
- ![イメージ説明](79367032c94eccc09389122b9762a08f.png)
41
+ title = models.CharField(max_length=200)
42
+
43
+
44
+
45
+ #このup_numberをどうにかしたい
46
+
47
+ up_number = models.IntegerField(default=0)
48
+
49
+
50
+
51
+ published_date = models.DateTimeField(blank=True, null=True)
52
+
53
+
54
+
55
+ def publish(self):
56
+
57
+ self.published_date = timezone.now()
58
+
59
+ self.save()
60
+
61
+
62
+
63
+ ```
28
64
 
29
65
 
30
66
 
@@ -32,21 +68,101 @@
32
68
 
33
69
 
34
70
 
35
- #forms.py
71
+ #blog/forms.py
36
72
 
73
+ ```python
74
+
37
- ![イメージ説明](e79402222eaf65ffb9b9bfc2909f7438.png)
75
+ from django import forms
38
76
 
39
77
 
40
78
 
41
- #post_list.html
79
+ from .models import Post
42
-
43
- ![イメージ説明](d46ebc083598e54f161897c0827b02ea.png)
44
80
 
45
81
 
46
82
 
47
- #views.py
83
+ class PostForm(forms.ModelForm):
48
84
 
85
+
86
+
87
+ up_number = forms.IntegerField()
88
+
89
+
90
+
91
+ ```
92
+
93
+
94
+
49
- ![イメージ説明](93dceecaea14d2a2316d50e0e133334e.png)
95
+ #blog/templates/blog/post_list.html
96
+
97
+ ```HTML
98
+
99
+ {% for post in posts %}
100
+
101
+ <form action="" method="POST">
102
+
103
+ {% csrf_token %}
104
+
105
+ <input type="submit" value="+1">
106
+
107
+ </form>
108
+
109
+ <div>
110
+
111
+ <h5>{{ post.up_number }}</h5>
112
+
113
+ </div>
114
+
115
+ {% endfor %}
116
+
117
+
118
+
119
+ ```
120
+
121
+
122
+
123
+ #blog/views.py
124
+
125
+ ```python
126
+
127
+ from django.shortcuts import render
128
+
129
+ from django.utils import timezone
130
+
131
+ from .forms import PostForm
132
+
133
+ from .models import Post
134
+
135
+
136
+
137
+ def post_list(request):
138
+
139
+ posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
140
+
141
+ return render(request, 'blog/post_list.html', {'posts': posts})
142
+
143
+
144
+
145
+ #分からないポイント
146
+
147
+ def post_form(request):
148
+
149
+
150
+
151
+ if request.method == 'POST':
152
+
153
+
154
+
155
+ post.up_number = int(post.cleaned_data['up_number']) + 1
156
+
157
+
158
+
159
+ post.save()
160
+
161
+
162
+
163
+
164
+
165
+ ```
50
166
 
51
167
 
52
168