質問編集履歴
3
teratailの使い方を間違ってました。自己解決の投稿。
test
CHANGED
File without changes
|
test
CHANGED
@@ -265,103 +265,3 @@
|
|
265
265
|
```
|
266
266
|
|
267
267
|
がありました。
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
# 自己解決しました。
|
272
|
-
|
273
|
-
## 下記修正は質問とは異なる修正も含まれますが、とにかくpostしたデータをデータベースに更新できたコードも含めて記載しておきます。
|
274
|
-
|
275
|
-
## testapp/user_views.py
|
276
|
-
|
277
|
-
###下記の修正はupdateが呼ばれなかったことに関する修正です。
|
278
|
-
|
279
|
-
``` python
|
280
|
-
|
281
|
-
def update(self, request, *args, **kwargs):
|
282
|
-
|
283
|
-
↓
|
284
|
-
|
285
|
-
def update(request):
|
286
|
-
|
287
|
-
```
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
下記はuser_views.pyのコードです。
|
292
|
-
|
293
|
-
``` python
|
294
|
-
|
295
|
-
def update(request):
|
296
|
-
|
297
|
-
print('updateに入った')
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
userid = request.POST.get('userid', None)
|
302
|
-
|
303
|
-
# filterを修正する時にググったコードをコピペしてしまったのがおかしかった。
|
304
|
-
|
305
|
-
# filter_containsをid__containsに修正しました。
|
306
|
-
|
307
|
-
# その後のイコールに余計なコードが入っていたので簡素化しました。
|
308
|
-
|
309
|
-
user = AppUser.objects.filter(id__contains=userid).first()
|
310
|
-
|
311
|
-
user.name = request.POST.get('username', None)
|
312
|
-
|
313
|
-
user.save()
|
314
|
-
|
315
|
-
# redirectのコードを簡素化
|
316
|
-
|
317
|
-
return HttpResponseRedirect('/')
|
318
|
-
|
319
|
-
```
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
下記はhtmlのテーブルのデータをpostした修正です。
|
324
|
-
|
325
|
-
## testapp/templates.html
|
326
|
-
|
327
|
-
``` html
|
328
|
-
|
329
|
-
<tbody>
|
330
|
-
|
331
|
-
{% for user in users %}
|
332
|
-
|
333
|
-
<form method="post" action="update/">
|
334
|
-
|
335
|
-
{% csrf_token %}
|
336
|
-
|
337
|
-
<tr>
|
338
|
-
|
339
|
-
<td>
|
340
|
-
|
341
|
-
{{ user.id }}
|
342
|
-
|
343
|
-
<input type="hidden" name="userid" id="userid" value="{{ user.id }}"/>
|
344
|
-
|
345
|
-
</td>
|
346
|
-
|
347
|
-
<td>
|
348
|
-
|
349
|
-
<input type="text" name="username" id="username" value="{{ user.name }}"/>
|
350
|
-
|
351
|
-
</td>
|
352
|
-
|
353
|
-
<td>{{ user.created_at }}</td>
|
354
|
-
|
355
|
-
<td>{{ user.updated_at }}</td>
|
356
|
-
|
357
|
-
<td><input type="submit" name="commit" value="更新" data-disable-with="更新" /></td>
|
358
|
-
|
359
|
-
</tr>
|
360
|
-
|
361
|
-
</form>
|
362
|
-
|
363
|
-
{% endfor %}
|
364
|
-
|
365
|
-
</tbody>
|
366
|
-
|
367
|
-
```
|
2
viewsのpostが呼ばれなかったことに関する修正。redirectを簡素化。
test
CHANGED
File without changes
|
test
CHANGED
@@ -265,3 +265,103 @@
|
|
265
265
|
```
|
266
266
|
|
267
267
|
がありました。
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
# 自己解決しました。
|
272
|
+
|
273
|
+
## 下記修正は質問とは異なる修正も含まれますが、とにかくpostしたデータをデータベースに更新できたコードも含めて記載しておきます。
|
274
|
+
|
275
|
+
## testapp/user_views.py
|
276
|
+
|
277
|
+
###下記の修正はupdateが呼ばれなかったことに関する修正です。
|
278
|
+
|
279
|
+
``` python
|
280
|
+
|
281
|
+
def update(self, request, *args, **kwargs):
|
282
|
+
|
283
|
+
↓
|
284
|
+
|
285
|
+
def update(request):
|
286
|
+
|
287
|
+
```
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
下記はuser_views.pyのコードです。
|
292
|
+
|
293
|
+
``` python
|
294
|
+
|
295
|
+
def update(request):
|
296
|
+
|
297
|
+
print('updateに入った')
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
userid = request.POST.get('userid', None)
|
302
|
+
|
303
|
+
# filterを修正する時にググったコードをコピペしてしまったのがおかしかった。
|
304
|
+
|
305
|
+
# filter_containsをid__containsに修正しました。
|
306
|
+
|
307
|
+
# その後のイコールに余計なコードが入っていたので簡素化しました。
|
308
|
+
|
309
|
+
user = AppUser.objects.filter(id__contains=userid).first()
|
310
|
+
|
311
|
+
user.name = request.POST.get('username', None)
|
312
|
+
|
313
|
+
user.save()
|
314
|
+
|
315
|
+
# redirectのコードを簡素化
|
316
|
+
|
317
|
+
return HttpResponseRedirect('/')
|
318
|
+
|
319
|
+
```
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
下記はhtmlのテーブルのデータをpostした修正です。
|
324
|
+
|
325
|
+
## testapp/templates.html
|
326
|
+
|
327
|
+
``` html
|
328
|
+
|
329
|
+
<tbody>
|
330
|
+
|
331
|
+
{% for user in users %}
|
332
|
+
|
333
|
+
<form method="post" action="update/">
|
334
|
+
|
335
|
+
{% csrf_token %}
|
336
|
+
|
337
|
+
<tr>
|
338
|
+
|
339
|
+
<td>
|
340
|
+
|
341
|
+
{{ user.id }}
|
342
|
+
|
343
|
+
<input type="hidden" name="userid" id="userid" value="{{ user.id }}"/>
|
344
|
+
|
345
|
+
</td>
|
346
|
+
|
347
|
+
<td>
|
348
|
+
|
349
|
+
<input type="text" name="username" id="username" value="{{ user.name }}"/>
|
350
|
+
|
351
|
+
</td>
|
352
|
+
|
353
|
+
<td>{{ user.created_at }}</td>
|
354
|
+
|
355
|
+
<td>{{ user.updated_at }}</td>
|
356
|
+
|
357
|
+
<td><input type="submit" name="commit" value="更新" data-disable-with="更新" /></td>
|
358
|
+
|
359
|
+
</tr>
|
360
|
+
|
361
|
+
</form>
|
362
|
+
|
363
|
+
{% endfor %}
|
364
|
+
|
365
|
+
</tbody>
|
366
|
+
|
367
|
+
```
|
1
models, views, templatesのディレクトリを分けてるので、質問がややこしくなってしまったようです。
test
CHANGED
File without changes
|
test
CHANGED
@@ -235,3 +235,33 @@
|
|
235
235
|
以上でした。
|
236
236
|
|
237
237
|
すみませんが、どうかよろしくお願いします。
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
### 質問(追記)
|
242
|
+
|
243
|
+
updateというメソッドを呼び出してmodelを更新したいのですが方法がわかりません。
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
情報が少し足りなかったかもしれませんので追記です。
|
248
|
+
|
249
|
+
testapp/urls.py
|
250
|
+
|
251
|
+
```
|
252
|
+
|
253
|
+
from django.conf.urls import url
|
254
|
+
|
255
|
+
from django.urls import include, path
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
urlpatterns = [
|
260
|
+
|
261
|
+
path('', include('testapp.views.urls')),
|
262
|
+
|
263
|
+
]
|
264
|
+
|
265
|
+
```
|
266
|
+
|
267
|
+
がありました。
|