質問編集履歴

2

views.pyの入力ミス

2021/06/18 04:20

投稿

TRUM
TRUM

スコア0

test CHANGED
File without changes
test CHANGED
@@ -136,12 +136,6 @@
136
136
 
137
137
 
138
138
 
139
- def get_success_url(self):
140
-
141
- return resolve_url('app:user_detail', pk=self.kwargs['pk'])
142
-
143
-
144
-
145
139
  def get_signed_url(request,pk):
146
140
 
147
141
  if request.method == 'POST' and request.is_ajax():
@@ -200,7 +194,7 @@
200
194
 
201
195
  ```html
202
196
 
203
- <form method="POST" enctype="multipart/form-data"action="{% url 'app:sign' user.pk %}" >
197
+ <form method="POST" enctype="multipart/form-data"action="{% url 'movie:sign' user.pk %}" >
204
198
 
205
199
  <div class="container mt-3">
206
200
 
@@ -280,7 +274,7 @@
280
274
 
281
275
  $.ajax({
282
276
 
283
- url: "{% url 'app:sign' user.pk %}",
277
+ url: "{% url 'movie:sign' user.pk %}",
284
278
 
285
279
  type: 'POST',
286
280
 

1

一つの回答を元に処理を行なったが新しい問題が発生したため

2021/06/18 04:20

投稿

TRUM
TRUM

スコア0

test CHANGED
File without changes
test CHANGED
@@ -97,3 +97,281 @@
97
97
  ###補足
98
98
 
99
99
  django-storageも導入しており、画像や動画を一時的に見ることができるURLがあるのでダウンロードに関する署名付きURLはできていると考えます。またこの方法でもAPP Engineではアップロードはできませんが、LocalHOST上ではうまく動くので余計混乱しています。
100
+
101
+
102
+
103
+ ###追記
104
+
105
+ attakei様のアドバイスを受けてjavascript(ajax)を利用したアップロードフォームをこちらの[サイト](http://kevindhawkins.com/blog/django-javascript-uploading-to-google-cloud-storage/)を参考に組み込んでみました。LocalhostでテストしてみるとPOST後の処理が正常に行われているのかが怪しいものとなります。どこが間違っているのかをご指摘いただければ幸いです。
106
+
107
+ ```views
108
+
109
+ class MovieUpload(OnlyYouMixin,generic.CreateView,):
110
+
111
+ template_name = 'app/movie_upload.html'
112
+
113
+ form_class = Upload
114
+
115
+
116
+
117
+ def form_valid(self,form,):
118
+
119
+ movie = form.save(commit=False)
120
+
121
+ movie.user = self.request.user
122
+
123
+ movie.save()
124
+
125
+
126
+
127
+
128
+
129
+ def get_success_url(self):
130
+
131
+ return resolve_url('movie:user_detail', pk=self.kwargs['pk'])
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+ def get_success_url(self):
140
+
141
+ return resolve_url('app:user_detail', pk=self.kwargs['pk'])
142
+
143
+
144
+
145
+ def get_signed_url(request,pk):
146
+
147
+ if request.method == 'POST' and request.is_ajax():
148
+
149
+ filename = request.POST.get('filename')
150
+
151
+ filetype = request.POST.get('type')
152
+
153
+ filesize = request.POST.get('size', 0)
154
+
155
+
156
+
157
+ # build the URL path using whatever information
158
+
159
+ fullpath = '/path/'+filename
160
+
161
+
162
+
163
+ # create the blob - the blob has to be created in order to get a signed URL
164
+
165
+ blob = default_storage.open(fullpath, 'wb')
166
+
167
+
168
+
169
+ # generate the signed URL through the blob object - don't use django-storages (yet)
170
+
171
+ signed_url = blob.blob.generate_signed_url(expiration=default_storage.expiration, method='PUT', content_type=filetype)
172
+
173
+
174
+
175
+ # This is what you'd do with djagno-storages
176
+
177
+ #signed_url = default_storage.url(fullpath)
178
+
179
+
180
+
181
+ # Send the signed URL back. I also send the path back because I want to display the uploaded image (relative path)
182
+
183
+ return JsonResponse({ 'signed_url': signed_url, 'url': settings.MEDIA_URL + fullpath })
184
+
185
+
186
+
187
+ # Probably a terrible way to respond. You do you.
188
+
189
+ return JsonResponse({})
190
+
191
+ ```
192
+
193
+ ```url
194
+
195
+ path('user_datail/User<int:pk>/upload',views.MovieUpload.as_view(),name='movie_upload_in'),
196
+
197
+ path('user_datail/User<int:pk>/upload/sign',views.get_signed_url,name='sign'),
198
+
199
+ ```
200
+
201
+ ```html
202
+
203
+ <form method="POST" enctype="multipart/form-data"action="{% url 'app:sign' user.pk %}" >
204
+
205
+ <div class="container mt-3">
206
+
207
+ <button id ="Upload"class="btn btn-info btn-lg" name="next">Upload</button>
208
+
209
+ <div class="field">
210
+
211
+ {{ form.title.label }}
212
+
213
+ {{ form.title }}
214
+
215
+ <span class="helptext">{{ form.title.help_text }}</span>
216
+
217
+ {{ form.title.errors }}
218
+
219
+ </div>
220
+
221
+ <div class="field">
222
+
223
+ {{ form.name.label }}
224
+
225
+ {{ form.name }}
226
+
227
+ </div>
228
+
229
+ <div class="field">
230
+
231
+ {{ form.publick.label }}
232
+
233
+ {{ form.publick }}
234
+
235
+ <label for="id_publick">Release</label> <span class="helptext"></span>
236
+
237
+ {{ form.publick.errors }}
238
+
239
+ </div>
240
+
241
+ <div class="field" id="cover_image">
242
+
243
+ {{ form.video.label_tag }}
244
+
245
+ {{ form.video }}
246
+
247
+ <span class="helptext">{{ form.video.help_text }}</span>
248
+
249
+ </div>
250
+
251
+
252
+
253
+ <script>
254
+
255
+ $('#id_video').fileupload({
256
+
257
+ // most of these doesn't matter and will be removed
258
+
259
+ // because the add() function does the actual upload now
260
+
261
+ dropZone: $('#id_video'),
262
+
263
+ add: function(e, data) {
264
+
265
+ $.each(data.files, function(index, file) {
266
+
267
+ // pack our data to get signature url
268
+
269
+ var formData = new FormData();
270
+
271
+ formData.append('filename', file.name);
272
+
273
+ formData.append('type', file.type);
274
+
275
+ formData.append('size', file.size);
276
+
277
+
278
+
279
+ // Step 3: get our signature URL
280
+
281
+ $.ajax({
282
+
283
+ url: "{% url 'app:sign' user.pk %}",
284
+
285
+ type: 'POST',
286
+
287
+ processData: false,
288
+
289
+ contentType: false,
290
+
291
+ dataType: 'json',
292
+
293
+ headers: {
294
+
295
+ 'X-CSRFToken': Cookies.get('csrftoken'),
296
+
297
+ },
298
+
299
+ data: formData
300
+
301
+ }).done(function (data) {
302
+
303
+ // Step 5: got our url, push to GCS
304
+
305
+ const xhr = new XMLHttpRequest();
306
+
307
+ if ('withCredentials' in xhr) {
308
+
309
+ xhr.open("PUT", data.signed_url, true);
310
+
311
+ }
312
+
313
+ else if (typeof XDomainRequest !== 'undefined') {
314
+
315
+ xhr = new XDomainRequest();
316
+
317
+ xhr.open("PUT", data.signed_url);
318
+
319
+ }
320
+
321
+ else {
322
+
323
+ xhr = null;
324
+
325
+ }
326
+
327
+
328
+
329
+ xhr.onload = () => {
330
+
331
+ const status = xhr.status;
332
+
333
+ if (status === 200) {
334
+
335
+ //alert("File is uploaded");
336
+
337
+ // show the image (uploadDone is separate function)
338
+
339
+ uploadDone(data.url)
340
+
341
+ } else {
342
+
343
+ alert("Failed to upload");
344
+
345
+ }
346
+
347
+ };
348
+
349
+
350
+
351
+ xhr.onerror = () => {
352
+
353
+ alert("Failed to upload");
354
+
355
+ };
356
+
357
+
358
+
359
+ xhr.setRequestHeader('Content-Type', file.type);
360
+
361
+ xhr.send(file);
362
+
363
+ });
364
+
365
+ });
366
+
367
+ },
368
+
369
+ done: function (e, data) {
370
+
371
+ }
372
+
373
+ });
374
+
375
+ </script>
376
+
377
+ ```