質問編集履歴

2

コード修正

2021/06/04 10:11

投稿

shunxile
shunxile

スコア26

test CHANGED
File without changes
test CHANGED
@@ -132,13 +132,25 @@
132
132
 
133
133
  return now - datetime.timedelta(days=1) <= self.pub_date <= now
134
134
 
135
+
136
+
137
+
138
+
135
139
  ```
136
140
 
137
141
  ```view
138
142
 
143
+ from django.urls import reverse
144
+
145
+ from django.http import HttpResponse, HttpResponseRedirect
146
+
147
+ from django.views.decorators.http import require_http_methods
148
+
149
+ from django.shortcuts import render,get_object_or_404, redirect
150
+
139
- from django.template import loaderview
151
+ from django.template import loader
140
-
152
+
141
- from .form import UpdateForm
153
+ from .form import EmployeeForm
142
154
 
143
155
 
144
156
 
@@ -150,29 +162,29 @@
150
162
 
151
163
  if id:
152
164
 
153
- update = get_object_or_404(User, pk=id)
165
+ employee = get_object_or_404(User, pk=id)
154
166
 
155
167
  else:
156
168
 
157
- update = User()
169
+ employee = User()
158
170
 
159
171
 
160
172
 
161
173
  if request.method == 'POST':
162
174
 
163
- form = UpdateForm(request.POST, instance=update)
175
+ form = EmployeeForm(request.POST, instance=employee)
164
176
 
165
177
  if form.is_valid():
166
178
 
167
- update = form.save(commit=False)
179
+ employee = form.save(commit=False)
168
-
180
+
169
- update.save()
181
+ employee.save()
170
182
 
171
183
  return redirect('employee:registration')
172
184
 
173
185
  else:
174
186
 
175
- form = UpdateForm(instance=update)
187
+ form = EmployeeForm(instance=employee)
176
188
 
177
189
 
178
190
 
@@ -184,29 +196,29 @@
184
196
 
185
197
  if id:
186
198
 
187
- update = get_object_or_404(User, pk=id)
199
+ employee = get_object_or_404(User, pk=id)
188
200
 
189
201
  else:
190
202
 
191
- update = User()
203
+ employee = User()
192
204
 
193
205
 
194
206
 
195
207
  if request.method == 'POST':
196
208
 
197
- form = UpdateForm(request.POST, instance=update)
209
+ form = EmployeeForm(request.POST, instance=employee)
198
210
 
199
211
  if form.is_valid():
200
212
 
201
- update = form.save(commit=False)
213
+ employee = form.save(commit=False)
202
-
214
+
203
- update.save()
215
+ employee.save()
204
-
216
+
205
- return redirect('employee:update')
217
+ return redirect('employee:update',id)
206
218
 
207
219
  else:
208
220
 
209
- form = UpdateForm(instance=update)
221
+ form = EmployeeForm(instance=employee)
210
222
 
211
223
 
212
224
 
@@ -216,12 +228,14 @@
216
228
 
217
229
  ```form
218
230
 
231
+ from django.db.models import fields
232
+
233
+ from employee.models import User
234
+
219
235
  from django import forms
220
236
 
221
237
  from django.forms import ModelForm
222
238
 
223
- from employee.models import User
224
-
225
239
  from django.core import validators
226
240
 
227
241
 
@@ -232,7 +246,7 @@
232
246
 
233
247
 
234
248
 
235
- class UpdateForm(ModelForm):
249
+ class EmployeeForm(ModelForm):
236
250
 
237
251
  class Meta:
238
252
 
@@ -240,7 +254,7 @@
240
254
 
241
255
  fields = ('employee_name','age','sex','department')
242
256
 
243
-
257
+
244
258
 
245
259
  employee_name = forms.CharField(required=False,label='担当者名',max_length=50)
246
260
 
@@ -296,17 +310,21 @@
296
310
 
297
311
 
298
312
 
313
+ # バリデーションで引っかかるためコメントアウトしてます
314
+
299
- def clean_department(self):
315
+ """ def clean_department(self):
300
316
 
301
317
  department = self.cleaned_data['department']
302
318
 
303
-     print(department) # コンソールでは管理を選択したに1と表示されています
319
+ # コンソールでは管理を選択したときに1と表示されています
320
+
321
+ print(department)
304
322
 
305
323
  if department != 1 and department != 2 and department != 3:
306
324
 
307
325
  raise forms.ValidationError("部署を選択してください")
308
326
 
309
- return department
327
+ return department """
310
328
 
311
329
 
312
330
 

1

分の修正

2021/06/04 10:11

投稿

shunxile
shunxile

スコア26

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- ユーザーの登録で正常な値を送っているのですが
5
+ ユーザーの登録で正常な値を送っているはずなのですが
6
6
 
7
7
  バリデーションに引っかかてしまい保存できません。
8
8