teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

views.pyの入力ミス

2021/06/18 04:20

投稿

TRUM
TRUM

スコア0

title CHANGED
File without changes
body CHANGED
@@ -67,9 +67,6 @@
67
67
 
68
68
 
69
69
 
70
- def get_success_url(self):
71
- return resolve_url('app:user_detail', pk=self.kwargs['pk'])
72
-
73
70
  def get_signed_url(request,pk):
74
71
  if request.method == 'POST' and request.is_ajax():
75
72
  filename = request.POST.get('filename')
@@ -99,7 +96,7 @@
99
96
  path('user_datail/User<int:pk>/upload/sign',views.get_signed_url,name='sign'),
100
97
  ```
101
98
  ```html
102
- <form method="POST" enctype="multipart/form-data"action="{% url 'app:sign' user.pk %}" >
99
+ <form method="POST" enctype="multipart/form-data"action="{% url 'movie:sign' user.pk %}" >
103
100
  <div class="container mt-3">
104
101
  <button id ="Upload"class="btn btn-info btn-lg" name="next">Upload</button>
105
102
  <div class="field">
@@ -139,7 +136,7 @@
139
136
 
140
137
  // Step 3: get our signature URL
141
138
  $.ajax({
142
- url: "{% url 'app:sign' user.pk %}",
139
+ url: "{% url 'movie:sign' user.pk %}",
143
140
  type: 'POST',
144
141
  processData: false,
145
142
  contentType: false,

1

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

2021/06/18 04:20

投稿

TRUM
TRUM

スコア0

title CHANGED
File without changes
body CHANGED
@@ -47,4 +47,143 @@
47
47
 
48
48
 
49
49
  ###補足
50
- django-storageも導入しており、画像や動画を一時的に見ることができるURLがあるのでダウンロードに関する署名付きURLはできていると考えます。またこの方法でもAPP Engineではアップロードはできませんが、LocalHOST上ではうまく動くので余計混乱しています。
50
+ django-storageも導入しており、画像や動画を一時的に見ることができるURLがあるのでダウンロードに関する署名付きURLはできていると考えます。またこの方法でもAPP Engineではアップロードはできませんが、LocalHOST上ではうまく動くので余計混乱しています。
51
+
52
+ ###追記
53
+ attakei様のアドバイスを受けてjavascript(ajax)を利用したアップロードフォームをこちらの[サイト](http://kevindhawkins.com/blog/django-javascript-uploading-to-google-cloud-storage/)を参考に組み込んでみました。LocalhostでテストしてみるとPOST後の処理が正常に行われているのかが怪しいものとなります。どこが間違っているのかをご指摘いただければ幸いです。
54
+ ```views
55
+ class MovieUpload(OnlyYouMixin,generic.CreateView,):
56
+ template_name = 'app/movie_upload.html'
57
+ form_class = Upload
58
+
59
+ def form_valid(self,form,):
60
+ movie = form.save(commit=False)
61
+ movie.user = self.request.user
62
+ movie.save()
63
+
64
+
65
+ def get_success_url(self):
66
+ return resolve_url('movie:user_detail', pk=self.kwargs['pk'])
67
+
68
+
69
+
70
+ def get_success_url(self):
71
+ return resolve_url('app:user_detail', pk=self.kwargs['pk'])
72
+
73
+ def get_signed_url(request,pk):
74
+ if request.method == 'POST' and request.is_ajax():
75
+ filename = request.POST.get('filename')
76
+ filetype = request.POST.get('type')
77
+ filesize = request.POST.get('size', 0)
78
+
79
+ # build the URL path using whatever information
80
+ fullpath = '/path/'+filename
81
+
82
+ # create the blob - the blob has to be created in order to get a signed URL
83
+ blob = default_storage.open(fullpath, 'wb')
84
+
85
+ # generate the signed URL through the blob object - don't use django-storages (yet)
86
+ signed_url = blob.blob.generate_signed_url(expiration=default_storage.expiration, method='PUT', content_type=filetype)
87
+
88
+ # This is what you'd do with djagno-storages
89
+ #signed_url = default_storage.url(fullpath)
90
+
91
+ # Send the signed URL back. I also send the path back because I want to display the uploaded image (relative path)
92
+ return JsonResponse({ 'signed_url': signed_url, 'url': settings.MEDIA_URL + fullpath })
93
+
94
+ # Probably a terrible way to respond. You do you.
95
+ return JsonResponse({})
96
+ ```
97
+ ```url
98
+ path('user_datail/User<int:pk>/upload',views.MovieUpload.as_view(),name='movie_upload_in'),
99
+ path('user_datail/User<int:pk>/upload/sign',views.get_signed_url,name='sign'),
100
+ ```
101
+ ```html
102
+ <form method="POST" enctype="multipart/form-data"action="{% url 'app:sign' user.pk %}" >
103
+ <div class="container mt-3">
104
+ <button id ="Upload"class="btn btn-info btn-lg" name="next">Upload</button>
105
+ <div class="field">
106
+ {{ form.title.label }}
107
+ {{ form.title }}
108
+ <span class="helptext">{{ form.title.help_text }}</span>
109
+ {{ form.title.errors }}
110
+ </div>
111
+ <div class="field">
112
+ {{ form.name.label }}
113
+ {{ form.name }}
114
+ </div>
115
+ <div class="field">
116
+ {{ form.publick.label }}
117
+ {{ form.publick }}
118
+ <label for="id_publick">Release</label> <span class="helptext"></span>
119
+ {{ form.publick.errors }}
120
+ </div>
121
+ <div class="field" id="cover_image">
122
+ {{ form.video.label_tag }}
123
+ {{ form.video }}
124
+ <span class="helptext">{{ form.video.help_text }}</span>
125
+ </div>
126
+
127
+ <script>
128
+ $('#id_video').fileupload({
129
+ // most of these doesn't matter and will be removed
130
+ // because the add() function does the actual upload now
131
+ dropZone: $('#id_video'),
132
+ add: function(e, data) {
133
+ $.each(data.files, function(index, file) {
134
+ // pack our data to get signature url
135
+ var formData = new FormData();
136
+ formData.append('filename', file.name);
137
+ formData.append('type', file.type);
138
+ formData.append('size', file.size);
139
+
140
+ // Step 3: get our signature URL
141
+ $.ajax({
142
+ url: "{% url 'app:sign' user.pk %}",
143
+ type: 'POST',
144
+ processData: false,
145
+ contentType: false,
146
+ dataType: 'json',
147
+ headers: {
148
+ 'X-CSRFToken': Cookies.get('csrftoken'),
149
+ },
150
+ data: formData
151
+ }).done(function (data) {
152
+ // Step 5: got our url, push to GCS
153
+ const xhr = new XMLHttpRequest();
154
+ if ('withCredentials' in xhr) {
155
+ xhr.open("PUT", data.signed_url, true);
156
+ }
157
+ else if (typeof XDomainRequest !== 'undefined') {
158
+ xhr = new XDomainRequest();
159
+ xhr.open("PUT", data.signed_url);
160
+ }
161
+ else {
162
+ xhr = null;
163
+ }
164
+
165
+ xhr.onload = () => {
166
+ const status = xhr.status;
167
+ if (status === 200) {
168
+ //alert("File is uploaded");
169
+ // show the image (uploadDone is separate function)
170
+ uploadDone(data.url)
171
+ } else {
172
+ alert("Failed to upload");
173
+ }
174
+ };
175
+
176
+ xhr.onerror = () => {
177
+ alert("Failed to upload");
178
+ };
179
+
180
+ xhr.setRequestHeader('Content-Type', file.type);
181
+ xhr.send(file);
182
+ });
183
+ });
184
+ },
185
+ done: function (e, data) {
186
+ }
187
+ });
188
+ </script>
189
+ ```