回答編集履歴

2

追記 2019/03/15 を追加しました。

2019/03/15 11:55

投稿

gh640
gh640

スコア1407

test CHANGED
@@ -99,3 +99,139 @@
99
99
 
100
100
 
101
101
  - [django/request.py at 2.1.7 · django/django · GitHub](https://github.com/django/django/blob/2.1.7/django/http/request.py#L321)
102
+
103
+
104
+
105
+ ### 追記 2019/03/15
106
+
107
+
108
+
109
+ > `staff_list = json.loads(request.body).get('staff_list', [])` にすると、staff_listの中身が空ではなくなったのですが、JSONDecodeErrorになってしまいます。
110
+
111
+ >
112
+
113
+ > ...
114
+
115
+ >
116
+
117
+ > 度々恐縮ですが、アドバイスいただけますと幸いです。。
118
+
119
+
120
+
121
+ コメントでいただいた追加のご質問に回答します。
122
+
123
+
124
+
125
+ こちら検索してみたところ、 `jQuery.ajax()` は `data` パラメータが文字列ではない場合に自動的に query string に変換する挙動をするようです。そして、今回の場合のように、 `data` の値が object で、その要素に array または object が含まれる場合には注意が必要とのことです。
126
+
127
+
128
+
129
+ 以下 jQuery 公式ドキュメントからの引用です(英語に抵抗があれば Google 翻訳等を使ってください)。
130
+
131
+
132
+
133
+ > data
134
+
135
+ >
136
+
137
+ > Type: `PlainObject` or `String` or `Array`
138
+
139
+ >
140
+
141
+ > Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See `processData` option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the `traditional` setting (described below).
142
+
143
+
144
+
145
+ - [jQuery.ajax() | jQuery API Documentation](http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings)
146
+
147
+
148
+
149
+ > **Note:** Because some frameworks have limited ability to parse serialized arrays, developers should exercise caution when passing an `obj` argument that contains objects or arrays nested within another array.
150
+
151
+ >
152
+
153
+ > **Note:** Because there is no universally agreed-upon specification for param strings, it is not possible to encode complex data structures using this method in a manner that works ideally across all languages supporting such input. Use JSON format as an alternative for encoding complex data instead.
154
+
155
+
156
+
157
+ - [jQuery.param() | jQuery API Documentation](http://api.jquery.com/jQuery.param/)
158
+
159
+
160
+
161
+ というわけで、フロント側とバックエンド側の両方で少し工夫が必要なようですね。すみません、私は jQuery で試していなかったので気が付きませんでした。
162
+
163
+
164
+
165
+ というわけで、具体的には、例えば JS 側を次のようにして
166
+
167
+
168
+
169
+ ```js
170
+
171
+ $.ajax({
172
+
173
+ 'url': '{% url "myapp:ajax_order_change" %}',
174
+
175
+ 'type': 'POST',
176
+
177
+ 'data': {
178
+
179
+ 'staff_list': JSON.stringify(staff_list),
180
+
181
+ },
182
+
183
+ 'dataType': 'json'
184
+
185
+ });
186
+
187
+ });
188
+
189
+ ```
190
+
191
+
192
+
193
+ バックエンド側を次のようにすると値が取得できるのではないかと思います。
194
+
195
+
196
+
197
+ ```python
198
+
199
+ import json
200
+
201
+ from django.http import QueryDict
202
+
203
+
204
+
205
+ def ajax_order_change(request, store_id):
206
+
207
+ # print() はデバッグが終われば削除してください
208
+
209
+ print(request.body)
210
+
211
+ params = QueryDict(request.body)
212
+
213
+ print(params)
214
+
215
+ staff_list = json.loads(params['order'])
216
+
217
+ print(staff_list)
218
+
219
+ // ...
220
+
221
+ ```
222
+
223
+
224
+
225
+ `request.body` が query string になって渡されるので `QueryDict` に渡して使うとやりやすいと思います。
226
+
227
+
228
+
229
+ あくまでもサンプルなので、コピペせずアイデアのみ参考にしてください。また、繰り返しになりますが、どのようなデータが来るかわからないという前提で、例外処理等は適切に行ってくださいね :D
230
+
231
+
232
+
233
+ この点についてはこれで解決するのではないかと思います。
234
+
235
+
236
+
237
+ もし他にも問題が出てきましたら、一対一でサポートし続けるのは難しいので、別の質問を立ててみてくださいー

1

追加のご質問を受けて追記 2019/03/14 を追加しました。

2019/03/15 11:55

投稿

gh640
gh640

スコア1407

test CHANGED
@@ -25,3 +25,77 @@
25
25
 
26
26
 
27
27
  - [php - jQuery UI Sortable, then write order into a database - Stack Overflow](https://stackoverflow.com/questions/15633341/jquery-ui-sortable-then-write-order-into-a-database)
28
+
29
+
30
+
31
+ ### 追記 2019/03/14
32
+
33
+
34
+
35
+ > フロント側からのデータを取得できない理由を教えていただけますと幸いです。
36
+
37
+
38
+
39
+ CSRF 設定( `@csrf_exempt` 等)は適切にされていて `ajax_order_change()` が実行されているのに `print(staff_list)` が空になる、ということですかね。
40
+
41
+
42
+
43
+ 公式ドキュメントを読むと
44
+
45
+
46
+
47
+ > If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead.
48
+
49
+
50
+
51
+ - [Request and response objects | Django documentation | Django](https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.HttpRequest.POST)
52
+
53
+
54
+
55
+ とのことで、このケースでは `request.POST` は使えないようですね。次のような感じで取得する必要があるのだと思います。
56
+
57
+
58
+
59
+ ```python
60
+
61
+ import json
62
+
63
+ staff_list = json.loads(request.body).get('staff_list', [])
64
+
65
+ ```
66
+
67
+
68
+
69
+ これ ↑ はあくまでも説明のための単純化したサンプルです。実際にはどのようなデータが来るかわからないという前提で、例外処理等は適切に行ってください。
70
+
71
+
72
+
73
+ 以下参考です。
74
+
75
+
76
+
77
+ Stack Overflow の質問:
78
+
79
+
80
+
81
+ - [Django & TastyPie: request.POST is empty - Stack Overflow](https://stackoverflow.com/questions/16213324/django-tastypie-request-post-is-empty)
82
+
83
+
84
+
85
+ 公式ドキュメント:
86
+
87
+
88
+
89
+ > request.POST will no longer include data posted via HTTP requests with non form-specific content-types in the header. In prior versions, data posted with content-types other than multipart/form-data or application/x-www-form-urlencoded would still end up represented in the request.POST attribute. Developers wishing to access the raw POST data for these cases, should use the request.body attribute instead.
90
+
91
+
92
+
93
+ - [Django 1.5 release notes | Django documentation | Django](https://docs.djangoproject.com/en/2.1/releases/1.5/#non-form-data-in-http-requests)
94
+
95
+
96
+
97
+ コード:
98
+
99
+
100
+
101
+ - [django/request.py at 2.1.7 · django/django · GitHub](https://github.com/django/django/blob/2.1.7/django/http/request.py#L321)