DjangoでPUTやDELETEやってみたメモ
Django でリクエストをPUTとかDELETEで投げる時に、 ちょろっとハマったのでメモ。
jQuery や Flash からPUTやDELETEでリクエストすると、 メソッド自体はちゃんと解釈されるものの、 なぜか渡したデータが取れなかった。 Pylons ではハマらなかったのになぁ。
で、そもそもデータが渡されてるのかを確かめるために、 Requestのraw_post_dataをプリントしてみると、 そこにはちゃんと入ってた。 データの受け渡し自体はちゃんと出来てるらしい。
じゃあ、次はどこでパースしてるか?って事で、 django.core.handlers.wsgi.py を追いかけてみる。
どうやら111行目あたり、 WSGIRequest のメソッドの _load_post_and_files がパースしてるみたいなのですが、 ここで原因を発見。
def _load_post_and_files(self):
# Populates self._post and self._files
if self.method == 'POST': #<-- ココ
if self.environ.get('CONTENT_TYPE', '').startswith('multipart'):
header_dict = dict([(k, v) for k, v in self.environ.items() if k.startswith('HTTP_')])
header_dict['Content-Type'] = self.environ.get('CONTENT_TYPE', '')
self._post, self._files = http.parse_file_upload(header_dict, self.raw_post_data)
else:
self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict()
else:
self._post, self._files = http.QueryDict('', encoding=self._encoding), datastructures.MultiValueDict()
どうやらPOSTじゃないとリクエストされたデータをパースしないらしいよ。困った。
django.core.handlers.modpython.py も見てみると、 こちらはContent-Typeのみを見て判別してるみたい。
とりあえず、POSTしか受け付けないのはツライので、 PUTとDELETEも追加して対処する事に。
Index: django/core/handlers/wsgi.py
===================================================================
--- django/core/handlers/wsgi.py (revision 6708)
+++ django/core/handlers/wsgi.py (working copy)
@@ -110,7 +110,7 @@
def _load_post_and_files(self):
# Populates self._post and self._files
- if self.method == 'POST':
+ if self.method in ('POST', 'PUT', 'DELETE'):
if self.environ.get('CONTENT_TYPE', '').startswith('multipart'):
header_dict = dict([(k, v) for k, v in self.environ.items() if k.startswith('HTTP_')])
header_dict['Content-Type'] = self.environ.get('CONTENT_TYPE', '')
とりあえず、コレでPUTやDELETEのデータもちゃんと受け取れるようになったよ。 果たして良いのかどうかは分からんですけども。
これでしばらく様子を見てみよう。
- Posted at:
- 2007/11/26 23:17:49
- 2 Comments
- 1 TrackBack
- Trackback:
- http://humming.via-kitchen.com/2007/11/26/tried-put-and-delete-on-django/trackback/
TrackBacks
[Django][Python][mechanize][jQuery][CSS][その他]巡回 - 常山日記
Blog: [Django][CORESERVER] django で SSL を扱いたい(その2) [Python][Django]Djangoはやたらモデルばかりが大きくなる [Python][Django]render_to_responseよりもdirect_to_template? Django sprint Django sprint at Exoweb offices WYSIWYG Editor(Editor HTML) no
- Created at:
- 2007/11/27 19:07:20
Comments
PUTやDELETEサポートするなら
レスポンスはちゃんと201(Created)や204(No Content)を返すようにしないとダメでしょ。
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6
nobu
ご指摘ありがとうございます。
その通りですね。レスポンスコードは重要だと認識しています。
実装するにあたってはしっかり調べていこうと思います。