質問編集履歴

3

追記分を削除

2019/01/16 14:24

投稿

kotatsu2
kotatsu2

スコア16

test CHANGED
File without changes
test CHANGED
@@ -155,385 +155,3 @@
155
155
 
156
156
 
157
157
  ```
158
-
159
- 2019/1/16 いただいた回答を以下に反映しましたので、ここから追記します
160
-
161
-
162
-
163
- 先の文には書いていませんでしたが、formは学生の新規追加、修正、スケジュールの新規追加、修正で、それぞれ作成して使用しています。
164
-
165
-
166
-
167
- 教えていただいた通り、student_detail_with_schedule関数を以下のように作成しましたが、
168
-
169
- TypeError at /schedule/4/student_detail_with_schedule/のエラーがでてしまいました。
170
-
171
-
172
-
173
- ```ここに言語を入力
174
-
175
- views.py
176
-
177
-
178
-
179
- def student_detail_with_schedule(request, student_id):
180
-
181
- """学生の情報を取得する"""
182
-
183
- try:
184
-
185
- student = Student.objects.get(id=student_id)
186
-
187
- except Student.DoesNotExist:
188
-
189
- raise Http404
190
-
191
-
192
-
193
-
194
-
195
- """学生のスケジュールを降順で一覧表示."""
196
-
197
- schedules = Schedule.objects.filter(student_id).order_by('-lesson_date') # 降順
198
-
199
-
200
-
201
- paginator = Paginator(schedules, 5)
202
-
203
-
204
-
205
- page = request.GET.get('page', 1)
206
-
207
-
208
-
209
- try:
210
-
211
- schedules = paginator.page(page)
212
-
213
- except (EmptyPage, PageNotAnInteger):
214
-
215
- schedules = paginator.page(1)
216
-
217
-
218
-
219
- return TemplateResponse(request, 'schedule/student_detail_with_schedule.html',
220
-
221
- {'schedules': schedules,
222
-
223
- 'student': student,})
224
-
225
-
226
-
227
- ```
228
-
229
- ```ここに言語を入力
230
-
231
- 【student_detail_with_schedule.html】
232
-
233
-
234
-
235
-
236
-
237
- {% extends 'schedule/base.html' %}
238
-
239
- {% load static %}
240
-
241
-
242
-
243
- {% block content %}
244
-
245
-
246
-
247
- <section>
248
-
249
- <div class="row">
250
-
251
- <div class="col-lg-12">
252
-
253
- <div class="page-header">
254
-
255
- <h4 class="mt-4 mb-5 border-bottom">Student detail</h4>
256
-
257
- </div>
258
-
259
- </div>
260
-
261
- </div>
262
-
263
- <div class="row">
264
-
265
- <div class="col-lg-12">
266
-
267
- <div class="bs-component">
268
-
269
-
270
-
271
- <div class="card border-secondary mb-3" style="max-width: 30rem;">
272
-
273
- <div class="card-header"> <h4>Name&nbsp;&nbsp;:&nbsp;&nbsp;{{ student.name }} </h4></div>
274
-
275
- <div class="card-body">
276
-
277
- <p class="card-text">Country&nbsp;&nbsp;:&nbsp;&nbsp;{{ student.country }}</p>
278
-
279
- </div>
280
-
281
- </div>
282
-
283
- </div>
284
-
285
- </div>
286
-
287
- </div>
288
-
289
- <div>
290
-
291
- <p>
292
-
293
- <hr>
294
-
295
- </p>
296
-
297
- <a href="{% url 'student_edit' student.id %}"><button type="button" class="btn btn-primary">編集</button></a>
298
-
299
- <a href="{% url 'student_delete' student.id %}"><button type="button" class="btn btn-danger">削除</button></a>
300
-
301
- <a href="{% url 'student_list' %}"><button type="button" class="btn btn-success">学生一覧画面に戻る</button></a>
302
-
303
- </div>
304
-
305
- </div>
306
-
307
- </section>
308
-
309
-
310
-
311
-
312
-
313
- <section>
314
-
315
- <div class="row">
316
-
317
- <div class="col-lg-12">
318
-
319
- <div class="page-header">
320
-
321
- <h4 class="mt-4 mb-5 border-bottom">スケジュール一覧</h4>
322
-
323
- </div>
324
-
325
- </div>
326
-
327
- </div>
328
-
329
-
330
-
331
- <div class="table-responsive">
332
-
333
- <table class="table table-hover">
334
-
335
- <thead>
336
-
337
- <tr class="table-active">
338
-
339
- <th scope="col" width="20%">Name</th>
340
-
341
- <th scope="col" width="20%">Date</th>
342
-
343
- <th scope="col" width="20%">Article</th>
344
-
345
- <th scope="col" width="40%">Content</th>
346
-
347
- </tr>
348
-
349
- </thead>
350
-
351
- <tbody>
352
-
353
- {% for schedule in schedules %}
354
-
355
- <tr>
356
-
357
- <td>{{ schedule.student }}</td>
358
-
359
- <td>{{ schedule.lesson_date }}</td>
360
-
361
- <td>{{ schedule.article }}</td>
362
-
363
- <td>{{ schedule.content }}</td>
364
-
365
- </tr>
366
-
367
- {% endfor %}
368
-
369
- </tbody>
370
-
371
- </table>
372
-
373
- </div>
374
-
375
-
376
-
377
- <p>
378
-
379
- <hr>
380
-
381
- </p>
382
-
383
-
384
-
385
- <div class="container-fluid">
386
-
387
- <div class="row">
388
-
389
- <div class="col-xs-3 col-sm-6 col-md-6 my-media-1">
390
-
391
- {% if schedules.has_previous %}
392
-
393
- <a href="?page={{ schedules.previous_page_number }}">&lt;&lt; Previous</a>
394
-
395
- {% else %}
396
-
397
- <span>&lt;&lt; Previous</span>
398
-
399
- {% endif %}
400
-
401
-
402
-
403
- &nbsp;&nbsp;|&nbsp;{{ schedules.number }}ページ&nbsp;&nbsp;|&nbsp;
404
-
405
-
406
-
407
- {% if schedules.has_next %}
408
-
409
- <a href="?page={{ schedules.next_page_number }}">Next &gt;&gt;</a>
410
-
411
- {% else %}
412
-
413
- <span>Next &gt;&gt;</span>
414
-
415
- {% endif %}
416
-
417
- </div>
418
-
419
-
420
-
421
- <div class="col-xs-offset-1 col-xs-3 col-sm-6 col-md-6 my-media-2">
422
-
423
- <a href="{% url 'schedule_create' %}"><button type="button" class="btn btn-primary">スケジュール新規追加</button></a>
424
-
425
- </div>
426
-
427
- </div>
428
-
429
- </div>
430
-
431
- </section>
432
-
433
-
434
-
435
-
436
-
437
- {% endblock %}
438
-
439
- ```
440
-
441
- 学生のIDとスケジュールのIDがぶつかっているようなのですが、修正方法を調べたのですが、どのように修正すればよいかわかりません。IDの変数を別名に変更したりしてみましたが、うまくいきません。
442
-
443
- プログラムを始めたばかりの未熟者なので、解決方法が全くわかりません。
444
-
445
-
446
-
447
- どの部分でエラーが起こっていますか?
448
-
449
- できればその原因を教えてほしいです。
450
-
451
- またどのように修正すればよいのか教えていただけませんか。
452
-
453
-
454
-
455
- 以下はエラー内容です。
456
-
457
- ```ここに言語を入力
458
-
459
- TypeError at /schedule/4/student_detail_with_schedule/
460
-
461
- student_detail_with_schedule() got an unexpected keyword argument 'schedule_id'
462
-
463
- Request Method:
464
-
465
- GET
466
-
467
- Request URL:
468
-
469
- http://127.0.0.1:8000/schedule/4/student_detail_with_schedule/
470
-
471
- Django Version:
472
-
473
- 2.1.5
474
-
475
- Exception Type:
476
-
477
- TypeError
478
-
479
- Exception Value:
480
-
481
- student_detail_with_schedule() got an unexpected keyword argument 'schedule_id'
482
-
483
- Exception Location:
484
-
485
- C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py in _get_response, line 124
486
-
487
- Python Executable:
488
-
489
- C:\Users\name\AppData\Local\Programs\Python\Python37\python.exe
490
-
491
- Python Version:
492
-
493
- 3.7.1
494
-
495
- Python Path:
496
-
497
- ['c:\python\practice\project',
498
-
499
- 'C:\Users\name\AppData\Local\Programs\Python\Python37\python37.zip',
500
-
501
- 'C:\Users\name\AppData\Local\Programs\Python\Python37\DLLs',
502
-
503
- 'C:\Users\name\AppData\Local\Programs\Python\Python37\lib',
504
-
505
- 'C:\Users\name\AppData\Local\Programs\Python\Python37',
506
-
507
- 'C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages']
508
-
509
- Server time:
510
-
511
- 水, 16 1月 2019 23:04:59 +0900
512
-
513
- Traceback Switch to copy-and-paste view
514
-
515
- C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py in inner
516
-
517
- response = get_response(request)
518
-
519
- ...
520
-
521
- ▶ Local vars
522
-
523
- C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py in _get_response
524
-
525
- response = self.process_exception_by_middleware(e, request)
526
-
527
- ...
528
-
529
- ▶ Local vars
530
-
531
- C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py in _get_response
532
-
533
- response = wrapped_callback(request, *callback_args, **callback_kwargs)
534
-
535
- ...
536
-
537
- ▶ Local vars
538
-
539
- ```

2

いただいた回答を反映しました

2019/01/16 14:24

投稿

kotatsu2
kotatsu2

スコア16

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- Djangoでレッスン記録を作成していす。
1
+ Djangoでレッスン記録を作成している、プログラム初心者です。
2
2
 
3
3
 
4
4
 
@@ -155,3 +155,385 @@
155
155
 
156
156
 
157
157
  ```
158
+
159
+ 2019/1/16 いただいた回答を以下に反映しましたので、ここから追記します
160
+
161
+
162
+
163
+ 先の文には書いていませんでしたが、formは学生の新規追加、修正、スケジュールの新規追加、修正で、それぞれ作成して使用しています。
164
+
165
+
166
+
167
+ 教えていただいた通り、student_detail_with_schedule関数を以下のように作成しましたが、
168
+
169
+ TypeError at /schedule/4/student_detail_with_schedule/のエラーがでてしまいました。
170
+
171
+
172
+
173
+ ```ここに言語を入力
174
+
175
+ views.py
176
+
177
+
178
+
179
+ def student_detail_with_schedule(request, student_id):
180
+
181
+ """学生の情報を取得する"""
182
+
183
+ try:
184
+
185
+ student = Student.objects.get(id=student_id)
186
+
187
+ except Student.DoesNotExist:
188
+
189
+ raise Http404
190
+
191
+
192
+
193
+
194
+
195
+ """学生のスケジュールを降順で一覧表示."""
196
+
197
+ schedules = Schedule.objects.filter(student_id).order_by('-lesson_date') # 降順
198
+
199
+
200
+
201
+ paginator = Paginator(schedules, 5)
202
+
203
+
204
+
205
+ page = request.GET.get('page', 1)
206
+
207
+
208
+
209
+ try:
210
+
211
+ schedules = paginator.page(page)
212
+
213
+ except (EmptyPage, PageNotAnInteger):
214
+
215
+ schedules = paginator.page(1)
216
+
217
+
218
+
219
+ return TemplateResponse(request, 'schedule/student_detail_with_schedule.html',
220
+
221
+ {'schedules': schedules,
222
+
223
+ 'student': student,})
224
+
225
+
226
+
227
+ ```
228
+
229
+ ```ここに言語を入力
230
+
231
+ 【student_detail_with_schedule.html】
232
+
233
+
234
+
235
+
236
+
237
+ {% extends 'schedule/base.html' %}
238
+
239
+ {% load static %}
240
+
241
+
242
+
243
+ {% block content %}
244
+
245
+
246
+
247
+ <section>
248
+
249
+ <div class="row">
250
+
251
+ <div class="col-lg-12">
252
+
253
+ <div class="page-header">
254
+
255
+ <h4 class="mt-4 mb-5 border-bottom">Student detail</h4>
256
+
257
+ </div>
258
+
259
+ </div>
260
+
261
+ </div>
262
+
263
+ <div class="row">
264
+
265
+ <div class="col-lg-12">
266
+
267
+ <div class="bs-component">
268
+
269
+
270
+
271
+ <div class="card border-secondary mb-3" style="max-width: 30rem;">
272
+
273
+ <div class="card-header"> <h4>Name&nbsp;&nbsp;:&nbsp;&nbsp;{{ student.name }} </h4></div>
274
+
275
+ <div class="card-body">
276
+
277
+ <p class="card-text">Country&nbsp;&nbsp;:&nbsp;&nbsp;{{ student.country }}</p>
278
+
279
+ </div>
280
+
281
+ </div>
282
+
283
+ </div>
284
+
285
+ </div>
286
+
287
+ </div>
288
+
289
+ <div>
290
+
291
+ <p>
292
+
293
+ <hr>
294
+
295
+ </p>
296
+
297
+ <a href="{% url 'student_edit' student.id %}"><button type="button" class="btn btn-primary">編集</button></a>
298
+
299
+ <a href="{% url 'student_delete' student.id %}"><button type="button" class="btn btn-danger">削除</button></a>
300
+
301
+ <a href="{% url 'student_list' %}"><button type="button" class="btn btn-success">学生一覧画面に戻る</button></a>
302
+
303
+ </div>
304
+
305
+ </div>
306
+
307
+ </section>
308
+
309
+
310
+
311
+
312
+
313
+ <section>
314
+
315
+ <div class="row">
316
+
317
+ <div class="col-lg-12">
318
+
319
+ <div class="page-header">
320
+
321
+ <h4 class="mt-4 mb-5 border-bottom">スケジュール一覧</h4>
322
+
323
+ </div>
324
+
325
+ </div>
326
+
327
+ </div>
328
+
329
+
330
+
331
+ <div class="table-responsive">
332
+
333
+ <table class="table table-hover">
334
+
335
+ <thead>
336
+
337
+ <tr class="table-active">
338
+
339
+ <th scope="col" width="20%">Name</th>
340
+
341
+ <th scope="col" width="20%">Date</th>
342
+
343
+ <th scope="col" width="20%">Article</th>
344
+
345
+ <th scope="col" width="40%">Content</th>
346
+
347
+ </tr>
348
+
349
+ </thead>
350
+
351
+ <tbody>
352
+
353
+ {% for schedule in schedules %}
354
+
355
+ <tr>
356
+
357
+ <td>{{ schedule.student }}</td>
358
+
359
+ <td>{{ schedule.lesson_date }}</td>
360
+
361
+ <td>{{ schedule.article }}</td>
362
+
363
+ <td>{{ schedule.content }}</td>
364
+
365
+ </tr>
366
+
367
+ {% endfor %}
368
+
369
+ </tbody>
370
+
371
+ </table>
372
+
373
+ </div>
374
+
375
+
376
+
377
+ <p>
378
+
379
+ <hr>
380
+
381
+ </p>
382
+
383
+
384
+
385
+ <div class="container-fluid">
386
+
387
+ <div class="row">
388
+
389
+ <div class="col-xs-3 col-sm-6 col-md-6 my-media-1">
390
+
391
+ {% if schedules.has_previous %}
392
+
393
+ <a href="?page={{ schedules.previous_page_number }}">&lt;&lt; Previous</a>
394
+
395
+ {% else %}
396
+
397
+ <span>&lt;&lt; Previous</span>
398
+
399
+ {% endif %}
400
+
401
+
402
+
403
+ &nbsp;&nbsp;|&nbsp;{{ schedules.number }}ページ&nbsp;&nbsp;|&nbsp;
404
+
405
+
406
+
407
+ {% if schedules.has_next %}
408
+
409
+ <a href="?page={{ schedules.next_page_number }}">Next &gt;&gt;</a>
410
+
411
+ {% else %}
412
+
413
+ <span>Next &gt;&gt;</span>
414
+
415
+ {% endif %}
416
+
417
+ </div>
418
+
419
+
420
+
421
+ <div class="col-xs-offset-1 col-xs-3 col-sm-6 col-md-6 my-media-2">
422
+
423
+ <a href="{% url 'schedule_create' %}"><button type="button" class="btn btn-primary">スケジュール新規追加</button></a>
424
+
425
+ </div>
426
+
427
+ </div>
428
+
429
+ </div>
430
+
431
+ </section>
432
+
433
+
434
+
435
+
436
+
437
+ {% endblock %}
438
+
439
+ ```
440
+
441
+ 学生のIDとスケジュールのIDがぶつかっているようなのですが、修正方法を調べたのですが、どのように修正すればよいかわかりません。IDの変数を別名に変更したりしてみましたが、うまくいきません。
442
+
443
+ プログラムを始めたばかりの未熟者なので、解決方法が全くわかりません。
444
+
445
+
446
+
447
+ どの部分でエラーが起こっていますか?
448
+
449
+ できればその原因を教えてほしいです。
450
+
451
+ またどのように修正すればよいのか教えていただけませんか。
452
+
453
+
454
+
455
+ 以下はエラー内容です。
456
+
457
+ ```ここに言語を入力
458
+
459
+ TypeError at /schedule/4/student_detail_with_schedule/
460
+
461
+ student_detail_with_schedule() got an unexpected keyword argument 'schedule_id'
462
+
463
+ Request Method:
464
+
465
+ GET
466
+
467
+ Request URL:
468
+
469
+ http://127.0.0.1:8000/schedule/4/student_detail_with_schedule/
470
+
471
+ Django Version:
472
+
473
+ 2.1.5
474
+
475
+ Exception Type:
476
+
477
+ TypeError
478
+
479
+ Exception Value:
480
+
481
+ student_detail_with_schedule() got an unexpected keyword argument 'schedule_id'
482
+
483
+ Exception Location:
484
+
485
+ C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py in _get_response, line 124
486
+
487
+ Python Executable:
488
+
489
+ C:\Users\name\AppData\Local\Programs\Python\Python37\python.exe
490
+
491
+ Python Version:
492
+
493
+ 3.7.1
494
+
495
+ Python Path:
496
+
497
+ ['c:\python\practice\project',
498
+
499
+ 'C:\Users\name\AppData\Local\Programs\Python\Python37\python37.zip',
500
+
501
+ 'C:\Users\name\AppData\Local\Programs\Python\Python37\DLLs',
502
+
503
+ 'C:\Users\name\AppData\Local\Programs\Python\Python37\lib',
504
+
505
+ 'C:\Users\name\AppData\Local\Programs\Python\Python37',
506
+
507
+ 'C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages']
508
+
509
+ Server time:
510
+
511
+ 水, 16 1月 2019 23:04:59 +0900
512
+
513
+ Traceback Switch to copy-and-paste view
514
+
515
+ C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py in inner
516
+
517
+ response = get_response(request)
518
+
519
+ ...
520
+
521
+ ▶ Local vars
522
+
523
+ C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py in _get_response
524
+
525
+ response = self.process_exception_by_middleware(e, request)
526
+
527
+ ...
528
+
529
+ ▶ Local vars
530
+
531
+ C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py in _get_response
532
+
533
+ response = wrapped_callback(request, *callback_args, **callback_kwargs)
534
+
535
+ ...
536
+
537
+ ▶ Local vars
538
+
539
+ ```

1

タイトルにDjangoを追加しました。

2019/01/16 14:09

投稿

kotatsu2
kotatsu2

スコア16

test CHANGED
@@ -1 +1 @@
1
- 2つのviews.pyの関数を1つのページに表示させたい。
1
+ Djangoで2つのviews.pyの関数を1つのページに表示させたい。
test CHANGED
File without changes