質問編集履歴

4

本文修正

2021/08/14 09:02

投稿

rain_history
rain_history

スコア0

test CHANGED
File without changes
test CHANGED
@@ -122,6 +122,8 @@
122
122
 
123
123
  ・urls.pyファイルにもstaticfilesの設定を以下のように追加。
124
124
 
125
+
126
+
125
127
  ・上記作業を行ったうえで、もう一度デプロイを行ってもエラーは変わらずでした。
126
128
 
127
129
  ・settings.pyの設定が誤っているのでしょうか……。どなたかご教示頂けますと幸いです。
@@ -142,6 +144,8 @@
142
144
 
143
145
  ・login.htmlを追加
144
146
 
147
+ ・whitenoiseはインストール済みです。 ⇒ whitenoise=5.2.0
148
+
145
149
 
146
150
 
147
151
 

3

本文修正

2021/08/14 09:02

投稿

rain_history
rain_history

スコア0

test CHANGED
File without changes
test CHANGED
@@ -136,7 +136,7 @@
136
136
 
137
137
  〈2021/8/14追記〉
138
138
 
139
- ・ディレク構造を追加
139
+ ・ディレクトリ構造を追加
140
140
 
141
141
  ・settings.pyを修正の上、全文を記載(セキュリティ上、一部は除く)
142
142
 

2

本文修正

2021/08/14 08:58

投稿

rain_history
rain_history

スコア0

test CHANGED
@@ -1 +1 @@
1
- 【Django】herokuデプロした結果、staticfilesが原因のサーバーエラー(500)が発生する
1
+ 【Django】本番環境静的ファルを配信することができない
test CHANGED
@@ -18,18 +18,100 @@
18
18
 
19
19
 
20
20
 
21
+ <2021/8/14追記〉
22
+
23
+ ・表題を変更しました。
24
+
25
+  【旧題】⇒【Django】herokuでデプロイした結果、staticfilesが原因のサーバーエラー(500)が発生する
26
+
27
+
28
+
29
+
30
+
21
31
  ### 発生している問題・エラーメッセージ
22
32
 
23
33
 
24
34
 
25
35
  ```Error
26
36
 
37
+ ValueError at /accounts/login/
38
+
27
39
  Missing staticfiles manifest entry for 'favicon.ico'
28
40
 
29
41
  ```
30
42
 
31
43
 
32
44
 
45
+ ```tree
46
+
47
+ PENTO_ARTS (1)
48
+
49
+ ├── accounts (2)
50
+
51
+ │ ├── __pycache__
52
+
53
+ │ ├── migrations
54
+
55
+ │ │ ├── __pycache__
56
+
57
+ │ │ └── __init__.py
58
+
59
+ │ └── templates
60
+
61
+ │ └── account
62
+
63
+ │ └── login.html
64
+
65
+ ├── pento_app (3)
66
+
67
+ │ ├── static
68
+
69
+ │ │ └── favicon.ico
70
+
71
+ │ └── templates
72
+
73
+ │ └── base.html
74
+
75
+ ├── pento_arts
76
+
77
+ │ ├── settings.py
78
+
79
+ │ └── urls.py
80
+
81
+
82
+
83
+ └── staticfiles
84
+
85
+ ├── admin
86
+
87
+ │ ├── css
88
+
89
+ │ ├── fonts
90
+
91
+ │ ├── img
92
+
93
+ │ │ └── gis
94
+
95
+ │ └── js
96
+
97
+ │ ├── admin
98
+
99
+ │ └── vendor
100
+
101
+ └── favicon.ico
102
+
103
+
104
+
105
+ ```
106
+
107
+ (1)PENTO_ARTS = プロジェクト名
108
+
109
+ (2)accounts = アプリ名
110
+
111
+ (3)pento_app = アプリ名
112
+
113
+
114
+
33
115
  ### 試したこと
34
116
 
35
117
 
@@ -52,6 +134,18 @@
52
134
 
53
135
 
54
136
 
137
+ 〈2021/8/14追記〉
138
+
139
+ ・ディレクり構造を追加
140
+
141
+ ・settings.pyを修正の上、全文を記載(セキュリティ上、一部は除く)
142
+
143
+ ・login.htmlを追加
144
+
145
+
146
+
147
+
148
+
55
149
  ※質問は初めてのため、情報がかなり不足しているかもしれません。必要に応じて追記させて頂きます。
56
150
 
57
151
 
@@ -64,6 +158,36 @@
64
158
 
65
159
  ```settings
66
160
 
161
+
162
+
163
+ import os
164
+
165
+ import django_heroku
166
+
167
+ from pathlib import Path
168
+
169
+ import environ
170
+
171
+
172
+
173
+ <省略>
174
+
175
+
176
+
177
+ # プロジェクトのベースフォルダを示す
178
+
179
+ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
180
+
181
+ REPOSITORY_ROOT = os.path.dirname(BASE_DIR)
182
+
183
+
184
+
185
+ #サーバが受理するサーバアドレスを指定する。この値を正確に記述することは「Hostヘッダインジェクション攻撃」に対して有効
186
+
187
+ ALLOWED_HOSTS = ["*"]
188
+
189
+
190
+
67
191
  INSTALLED_APPS = [
68
192
 
69
193
  'pento_app.apps.PentoAppConfig',
@@ -98,13 +222,35 @@
98
222
 
99
223
 
100
224
 
225
+ MIDDLEWARE = [
226
+
227
+ 'django.middleware.security.SecurityMiddleware',
228
+
229
+ 'django.contrib.sessions.middleware.SessionMiddleware',
230
+
231
+ 'django.middleware.common.CommonMiddleware',
232
+
233
+ 'django.middleware.csrf.CsrfViewMiddleware',
234
+
235
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
236
+
237
+ 'django.contrib.messages.middleware.MessageMiddleware',
238
+
239
+ 'django.middleware.clickjacking.XFrameOptionsMiddleware',
240
+
241
+ 'whitenoise.middleware.WhiteNoiseMiddleware',
242
+
243
+ ]
244
+
245
+
246
+
101
247
  <省略>
102
248
 
103
249
 
104
250
 
105
- STATIC_URL = '/staticfiles/'
251
+ STATIC_URL = '/static/'
106
-
107
- STATICFILES_DIRS = [os.path.join(BASE_DIR, 'staticfiles')]
252
+
253
+
108
254
 
109
255
  STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
110
256
 
@@ -112,12 +258,22 @@
112
258
 
113
259
 
114
260
 
261
+ django_heroku.settings(locals())
262
+
263
+
264
+
115
265
  ```
116
266
 
117
267
 
118
268
 
119
269
  ```urls
120
270
 
271
+
272
+
273
+ from django.contrib.staticfiles.urls import staticfiles_urlpatterns
274
+
275
+
276
+
121
277
  urlpatterns = [
122
278
 
123
279
 
@@ -126,13 +282,21 @@
126
282
 
127
283
 
128
284
 
129
- ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
285
+ ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
286
+
287
+
288
+
130
-
289
+ urlpatterns += staticfiles_urlpatterns()
131
-
132
290
 
133
291
  urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
134
292
 
293
+
294
+
295
+
296
+
297
+ from pento_app.views import my_customized_server_error
298
+
135
- urlpatterns += staticfiles_urlpatterns()
299
+ handler500 = my_customized_server_error
136
300
 
137
301
 
138
302
 
@@ -328,6 +492,74 @@
328
492
 
329
493
  ```
330
494
 
495
+ ```login
496
+
497
+ {% extends 'base.html' %}
498
+
499
+ {% load static %}
500
+
501
+ {% load bootstrap4 %}
502
+
503
+ {% block Title %}<title>PENTO-ARTS | ログイン</title>{% endblock Title %}
504
+
505
+
506
+
507
+ {% block content %}
508
+
509
+
510
+
511
+ <link rel="stylesheet" href="{% static 'style.css' %}">
512
+
513
+
514
+
515
+ <div class="form-content">
516
+
517
+ <h1 class="mb-4">ログイン</h1>
518
+
519
+
520
+
521
+ {% if messages %}
522
+
523
+ <ul>
524
+
525
+ {% for message in messages %}
526
+
527
+ <li class="message_fontcolor">{{ message }}</li>
528
+
529
+ {% endfor %}
530
+
531
+ </ul>
532
+
533
+ {% endif %}
534
+
535
+
536
+
537
+ <form method="post" action="{% url 'account_login' %}">
538
+
539
+ {% csrf_token %}
540
+
541
+ {% bootstrap_form form %}
542
+
543
+ <button class="btn btn-info btn-block" type="submit">ログイン</button>
544
+
545
+ <p class="mt-3"><a href="{% url 'account_signup' %}" class="text-info">新規登録はこちら</a></p>
546
+
547
+ <!--※パスワードリセット機能は一旦保留
548
+
549
+ <p class="mt-3"><a href="{% url 'account_reset_password' %}" class="text-info">パスワードをお忘れですか?</a></p>
550
+
551
+ -->
552
+
553
+ </form>
554
+
555
+ </div>
556
+
557
+ {% endblock content %}
558
+
559
+
560
+
561
+ ```
562
+
331
563
 
332
564
 
333
565
 

1

base.htmlのコードを追記

2021/08/14 08:57

投稿

rain_history
rain_history

スコア0

test CHANGED
File without changes
test CHANGED
@@ -46,6 +46,12 @@
46
46
 
47
47
 
48
48
 
49
+ 〈2021/8/11追記〉
50
+
51
+ ・favicon.icoの記述箇所『base.html』のコードを追記しました。
52
+
53
+
54
+
49
55
  ※質問は初めてのため、情報がかなり不足しているかもしれません。必要に応じて追記させて頂きます。
50
56
 
51
57
 
@@ -134,6 +140,196 @@
134
140
 
135
141
 
136
142
 
143
+ ```base
144
+
145
+
146
+
147
+ {% load static %}
148
+
149
+ {% load bootstrap4 %}
150
+
151
+
152
+
153
+ <!DOCTYPE html>
154
+
155
+ <html lang="ja">
156
+
157
+
158
+
159
+ <head>
160
+
161
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
162
+
163
+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
164
+
165
+ <link rel="shortcut icon" href="{% static 'favicon.ico' %}">
166
+
167
+ <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
168
+
169
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
170
+
171
+ <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
172
+
173
+
174
+
175
+ <link rel="stylesheet" type="text/css" href="{% static 'header.css' %}">
176
+
177
+ <link rel="stylesheet" type="text/css" href="{% static 'footer.css' %}">
178
+
179
+ <meta charset="UTF-8">
180
+
181
+ <meta name="viewport" content="width=device-width,initial-scale=1">
182
+
183
+
184
+
185
+ {% block Title %}<title>PENTO-ARTS</title>{% endblock Title %}
186
+
187
+
188
+
189
+ <link href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/css/lightbox.css" rel="stylesheet">
190
+
191
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
192
+
193
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.7.1/js/lightbox.min.js" type="text/javascript"></script>
194
+
195
+
196
+
197
+ </head>
198
+
199
+
200
+
201
+ <body>
202
+
203
+
204
+
205
+ <!-- Header Start -->
206
+
207
+ <header class="site-header">
208
+
209
+ <div class="wrapper site-header__wrapper">
210
+
211
+ <h2 class="brand">PENTO-ARTS</h2>
212
+
213
+ </div>
214
+
215
+ </header>
216
+
217
+ <!-- Header End -->
218
+
219
+
220
+
221
+ {% block content %}
222
+
223
+ {% endblock content %}
224
+
225
+
226
+
227
+ <footer id="footer01" class="footer outer-block">
228
+
229
+
230
+
231
+ <p class="copyright">
232
+
233
+ <省略>
234
+
235
+ </p>
236
+
237
+ </footer>
238
+
239
+
240
+
241
+ </body>
242
+
243
+ <script>
244
+
245
+ $.ajaxSetup({
246
+
247
+ beforeSend: function (xhr, settings) {
248
+
249
+ function getCookie(name) {
250
+
251
+ var cookieValue = null;
252
+
253
+ if (document.cookie && document.cookie != '') {
254
+
255
+ var cookies = document.cookie.split(';');
256
+
257
+ for (var i = 0; i < cookies.length; i++) {
258
+
259
+ var cookie = jQuery.trim(cookies[i]);
260
+
261
+ if (cookie.substring(0, name.length + 1) == (name + '=')) {
262
+
263
+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
264
+
265
+ break;
266
+
267
+ }
268
+
269
+ }
270
+
271
+ }
272
+
273
+ return cookieValue;
274
+
275
+ }
276
+
277
+ if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
278
+
279
+ xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
280
+
281
+ }
282
+
283
+ }
284
+
285
+ });
286
+
287
+ $(document).on("click", ".post-like", function () {
288
+
289
+ var id = $(this).data('id');
290
+
291
+ $.ajax({
292
+
293
+ type: "post",
294
+
295
+ url: '{% url "pento_app:like" %}',
296
+
297
+ data: {
298
+
299
+ id: id,
300
+
301
+ csrfmiddlewaretoken: $("#csrfmiddlewaretoken").val()
302
+
303
+ },
304
+
305
+ success: function (data) {
306
+
307
+ $("#post-like-" + id).removeClass("post-like text-info").addClass("post-liked disabled text-secondary");
308
+
309
+ var like_count = data["like_count"]
310
+
311
+ $("#like-count-" + id).html(like_count);
312
+
313
+ alert(data["message"])
314
+
315
+ }
316
+
317
+ });
318
+
319
+ });
320
+
321
+ </script>
322
+
323
+
324
+
325
+ </html>
326
+
327
+
328
+
329
+ ```
330
+
331
+
332
+
137
333
 
138
334
 
139
335