質問編集履歴

1

追加ファイルとファイル構造を記載しました。

2021/03/05 08:46

投稿

mAsuo_pro
mAsuo_pro

スコア0

test CHANGED
@@ -1 +1 @@
1
- [Django] jsファイルが反応しない
1
+ djangojsファイルが反応しない
test CHANGED
@@ -10,10 +10,46 @@
10
10
 
11
11
 
12
12
 
13
+
14
+
13
15
  エラーメッセージはありませんが、デバッグをfluseにすると500エラーが発生します。
14
16
 
15
17
 
16
18
 
19
+ **試したこと**
20
+
21
+ ・スーパーリロード
22
+
23
+ ・django-static-md5urlを使う
24
+
25
+ ・キャッシュバスティングの設定(500エラーの発生)
26
+
27
+
28
+
29
+ **ファイル構造**
30
+
31
+ cissapp/
32
+
33
+ ├ciss
34
+
35
+ | └views.py
36
+
37
+ ├static
38
+
39
+ | └js
40
+
41
+ | └vote.js
42
+
43
+ ├template
44
+
45
+ | └detail.html
46
+
47
+
48
+
49
+
50
+
51
+
52
+
17
53
  views.py
18
54
 
19
55
  ```views.py
@@ -86,16 +122,6 @@
86
122
 
87
123
  ```models.py
88
124
 
89
- from django.db import models
90
-
91
- from django.utils import timezone
92
-
93
- from django.contrib.auth import get_user_model
94
-
95
-
96
-
97
-
98
-
99
125
  class Data(models.Model):
100
126
 
101
127
  category = models.CharField("科目種別", blank=True, max_length=100)
@@ -108,19 +134,7 @@
108
134
 
109
135
  name = models.CharField("科目", blank=True, max_length=20)
110
136
 
111
- period = models.CharField("時限", blank=True, max_length=2)
137
+
112
-
113
- teacher = models.CharField("担当教員", blank=True, max_length=30)
114
-
115
- credit = models.CharField("単位", blank=True, max_length=1)
116
-
117
-
118
-
119
- class Meta:
120
-
121
- verbose_name = '授業'
122
-
123
- verbose_name_plural = '授業一覧'
124
138
 
125
139
 
126
140
 
@@ -130,34 +144,6 @@
130
144
 
131
145
 
132
146
 
133
-
134
-
135
- # Create your models here.
136
-
137
- class TopicManager(models.Manager):
138
-
139
- # Topic操作に関する処理を追加
140
-
141
- pass
142
-
143
-
144
-
145
- class CommentManager(models.Manager):
146
-
147
- # Comment操作に関する処理を追加
148
-
149
- pass
150
-
151
-
152
-
153
- class CategoryManager(models.Manager):
154
-
155
- # Category操作に関する処理を追加
156
-
157
- pass
158
-
159
-
160
-
161
147
  class Topic(models.Model):
162
148
 
163
149
  id = models.BigAutoField(
@@ -228,22 +214,6 @@
228
214
 
229
215
  )
230
216
 
231
- modified = models.DateTimeField(
232
-
233
- auto_now=True,
234
-
235
- )
236
-
237
- objects = TopicManager()
238
-
239
-
240
-
241
- class Meta:
242
-
243
- verbose_name = '投稿'
244
-
245
- verbose_name_plural = '投稿一覧'
246
-
247
217
 
248
218
 
249
219
 
@@ -380,33 +350,7 @@
380
350
 
381
351
  ```ここに言語を入力
382
352
 
383
- import os
353
+
384
-
385
- from pathlib import Path
386
-
387
-
388
-
389
- # Build paths inside the project like this: BASE_DIR / 'subdir'.
390
-
391
- BASE_DIR = Path(__file__).resolve().parent.parent
392
-
393
-
394
-
395
-
396
-
397
- # Quick-start development settings - unsuitable for production
398
-
399
- # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
400
-
401
-
402
-
403
- # SECURITY WARNING: keep the secret key used in production secret!
404
-
405
- SECRET_KEY = 'hiv6*!i=apr-fcaxpahnt!*!(up^&2r)p57z0=d$t*pk6*izv@'
406
-
407
-
408
-
409
- # SECURITY WARNING: don't run with debug turned on in production!
410
354
 
411
355
  DEBUG = True
412
356
 
@@ -416,12 +360,6 @@
416
360
 
417
361
 
418
362
 
419
-
420
-
421
- # Application definition
422
-
423
-
424
-
425
363
  INSTALLED_APPS = [
426
364
 
427
365
  'accounts.apps.AccountsConfig',
@@ -458,178 +396,166 @@
458
396
 
459
397
 
460
398
 
399
+ STATIC_URL = '/static/'
400
+
461
- MIDDLEWARE = [
401
+ STATICFILES_DIRS = [
462
-
463
- 'django.middleware.security.SecurityMiddleware',
402
+
464
-
465
- 'django.contrib.sessions.middleware.SessionMiddleware',
466
-
467
- 'django.middleware.common.CommonMiddleware',
403
+ os.path.join(BASE_DIR, 'static'),
468
-
469
- 'django.middleware.csrf.CsrfViewMiddleware',
470
-
471
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
472
-
473
- 'django.contrib.messages.middleware.MessageMiddleware',
474
-
475
- 'django.middleware.clickjacking.XFrameOptionsMiddleware',
476
-
477
- 'debug_toolbar.middleware.DebugToolbarMiddleware', # Deubg tool bar
478
404
 
479
405
  ]
480
406
 
481
-
482
-
483
- ROOT_URLCONF = 'CISS.urls'
484
-
485
-
486
-
487
- TEMPLATES = [
488
-
489
- {
490
-
491
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
492
-
493
- 'DIRS': [os.path.join(BASE_DIR, 'templates')],
494
-
495
- 'APP_DIRS': True,
496
-
497
- 'OPTIONS': {
498
-
499
- 'context_processors': [
500
-
501
- 'django.template.context_processors.debug',
502
-
503
- 'django.template.context_processors.request',
504
-
505
- 'django.contrib.auth.context_processors.auth',
506
-
507
- 'django.contrib.messages.context_processors.messages',
508
-
509
- ],
510
-
511
- },
512
-
513
- },
514
-
515
- ]
516
-
517
-
518
-
519
- LOGIN_REDIRECT_URL = '/'
520
-
521
-
522
-
523
- WSGI_APPLICATION = 'CISS.wsgi.application'
524
-
525
-
526
-
527
-
528
-
529
- # Database
530
-
531
- # https://docs.djangoproject.com/en/3.1/ref/settings/#databases
532
-
533
-
534
-
535
- DATABASES = {
536
-
537
- 'default': {
538
-
539
- 'ENGINE': 'django.db.backends.sqlite3',
540
-
541
- 'NAME': BASE_DIR / 'db.sqlite3',
407
+ ```
408
+
409
+ vote.js
410
+
411
+ ```ここに言語を入力
412
+
413
+ $(function(){
414
+
415
+ // setup for ajax
416
+
417
+ var csrftoken = getCookie('csrftoken');
418
+
419
+ $.ajaxSetup({
420
+
421
+ beforeSend: function(xhr, settings) {
422
+
423
+ if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
424
+
425
+ xhr.setRequestHeader("X-CSRFToken", csrftoken);
426
+
427
+ }
428
+
429
+ }
430
+
431
+ });
432
+
433
+
434
+
435
+ var votedList = [];// 連打防止用のコメントID格納リスト
436
+
437
+ // いいねボタン押下時の処理
438
+
439
+ onClickVoteButton();
440
+
441
+
442
+
443
+ function getCookie(name) {
444
+
445
+ var cookieValue = null;
446
+
447
+ if (document.cookie && document.cookie !== '') {
448
+
449
+ var cookies = document.cookie.split(';');
450
+
451
+ for (var i = 0; i < cookies.length; i++) {
452
+
453
+ var cookie = jQuery.trim(cookies[i]);
454
+
455
+ // Does this cookie string begin with the name we want?
456
+
457
+ if (cookie.substring(0, name.length + 1) === (name + '=')) {
458
+
459
+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
460
+
461
+ break;
462
+
463
+ }
464
+
465
+ }
466
+
467
+ }
468
+
469
+ return cookieValue;
542
470
 
543
471
  }
544
472
 
545
- }
546
-
547
-
548
-
549
-
550
-
551
- # Password validation
552
-
553
- # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
554
-
555
-
556
-
557
- AUTH_PASSWORD_VALIDATORS = [
558
-
559
- {
560
-
561
- 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
562
-
563
- },
564
-
565
- {
566
-
567
- 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
568
-
569
- },
570
-
571
- {
572
-
573
- 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
574
-
575
- },
576
-
577
- {
578
-
579
- 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
580
-
581
- },
582
-
583
- ]
584
-
585
-
586
-
587
-
588
-
589
- # Internationalization
590
-
591
- # https://docs.djangoproject.com/en/3.1/topics/i18n/
592
-
593
-
594
-
595
- LANGUAGE_CODE = 'ja'
596
-
597
-
598
-
599
- TIME_ZONE = 'Asia/Tokyo'
600
-
601
-
602
-
603
- USE_I18N = True
604
-
605
-
606
-
607
- USE_L10N = True
608
-
609
-
610
-
611
- USE_TZ = True
612
-
613
-
614
-
615
-
616
-
617
- # Static files (CSS, JavaScript, Images)
618
-
619
- # https://docs.djangoproject.com/en/3.1/howto/static-files/
620
-
621
-
622
-
623
- STATIC_URL = '/static/'
624
-
625
- STATICFILES_DIRS = [
626
-
627
- os.path.join(BASE_DIR, 'static'),
628
-
629
- ]
630
-
631
-
632
-
633
- #STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
473
+
474
+
475
+ function csrfSafeMethod(method) {
476
+
477
+ // these HTTP methods do not require CSRF protection
478
+
479
+ return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
480
+
481
+ }
482
+
483
+
484
+
485
+ function onClickVoteButton() {
486
+
487
+ $('.vote_button').on('click', function() {
488
+
489
+ var commentId = $(this).data('comment-id');
490
+
491
+ var currentCount = $(this).data('count');
492
+
493
+ var countViewer = $(this).find('.vote_counter');
494
+
495
+ if (votedList.indexOf(commentId) < 0) {
496
+
497
+ vote(commentId, currentCount, countViewer);
498
+
499
+ }
500
+
501
+ });
502
+
503
+ }
504
+
505
+
506
+
507
+ // ajax通信して投票結果を反映する
508
+
509
+ function vote(commentId, currentCount, countViewer) {
510
+
511
+ let url = '/api/v1/vote/';
512
+
513
+ $.ajax({
514
+
515
+ type: 'POST',
516
+
517
+ url: url,
518
+
519
+ data: {
520
+
521
+ comment_id: commentId
522
+
523
+ }
524
+
525
+ }).then(
526
+
527
+ data => {
528
+
529
+ if (data.result) {
530
+
531
+ countViewer.text(currentCount + 1);
532
+
533
+ votedList.push(commentId);
534
+
535
+ }
536
+
537
+ },
538
+
539
+ error => {
540
+
541
+ if (error.responseJSON.message) {
542
+
543
+ alert(error.responseJSON.message);
544
+
545
+ }
546
+
547
+ }
548
+
549
+ );
550
+
551
+ }
552
+
553
+
554
+
555
+
556
+
557
+ });
558
+
559
+
634
560
 
635
561
  ```