2013-07-02 26 views
0

我有一個django視圖定義如下。Django Class Based View正在返回一個空的POST詞典

class SomeView(View): 

    def post(self, request, *args, **kwargs): 
     print "###############################" 
     print request.POST 
     print "###############################" 
     return HttpResponse(json.dumps(kwargs), mimetype='application/json') 

當我使任意捲曲請求,如

curl -X POST -H "Content-Type: application/json" '{"hello":"world"}' http://localhost:8000/blog/ 

我給定一個空的字典作爲響應,與下面的控制檯輸出

############################### 
<QueryDict: {}> 
############################### 

這是爲什麼圖不接受任何數據?此外,應該指出的是,我註釋掉

'django.middleware.csrf.CsrfViewMiddleware', 

當我打電話self.request獲取,我得到下面的輸出

################################# 
<WSGIRequest 
path:/blog/, 
GET:<QueryDict: {}>, 
POST:<QueryDict: {}>, 
COOKIES:{}, 
META:{'CCACHE_HASHDIR': '', 
'COLORTERM': 'gnome-terminal', 
'CONTENT_LENGTH': '16', 
'CONTENT_TYPE': 'application/json', 
'CVS_RSH': 'ssh', 
'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-FukIqsBHiS,guid=2bc53020b746b1753871d45a51ce0be2', 
'DESKTOP_SESSION': 'gnome', 
'DISPLAY': ':0', 
'DJANGO_SETTINGS_MODULE': 'mongoblog.settings', 
'GATEWAY_INTERFACE': 'CGI/1.1', 
'GDMSESSION': 'gnome', 
'GJS_DEBUG_OUTPUT': 'stderr', 
'GJS_DEBUG_TOPICS': 'JS ERROR;JS LOG', 
'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 
'GNOME_KEYRING_CONTROL': '/run/user/1000/keyring-wmgVNG', 
'GNOME_KEYRING_PID': '1262', 
'GPG_AGENT_INFO': '/run/user/1000/keyring-wmgVNG/gpg:0:1', 
'HISTCONTROL': 'ignoredups', 
'HISTSIZE': '1000', 
'HOME': '/home/username', 
'HOSTNAME': 'localhost.localdomain', 
'HTTP_ACCEPT': '*/*', 
'HTTP_HOST': 'localhost:8000', 
'HTTP_USER_AGENT': 'curl/7.27.0', 
'IMSETTINGS_INTEGRATE_DESKTOP': 'yes', 
'IMSETTINGS_MODULE': 'none', 
'KRB5CCNAME': 'DIR:/run/user/1000/krb5cc_a3b7d843fdc5b71dcc827ae551ce0bd1', 
'LANG': 'en_US.UTF-8', 
'LESSOPEN': '||/usr/bin/lesspipe.sh %s', 
'LOGNAME': 'username', 
'LS_COLORS': 'rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lz=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.bz=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:', 
'MAIL': '/var/spool/mail/username', 
'OLDPWD': '/home/username/Programming/Python/Django/MongoRest/mongoblog/blog', 
'PATH': '/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/username/.local/bin:/home/username/bin', 
'PATH_INFO': u'/blog/', 
'PWD': '/home/username/Programming/Python/Django/MongoRest/mongoblog', 
'QTDIR': '/usr/lib64/qt-3.3', 
'QTINC': '/usr/lib64/qt-3.3/include', 
'QTLIB': '/usr/lib64/qt-3.3/lib', 
'QT_GRAPHICSSYSTEM_CHECKED': '1', 
'QT_IM_MODULE': 'ibus', 
'QUERY_STRING': '', 
'REMOTE_ADDR': '127.0.0.1', 
'REMOTE_HOST': '', 
'REQUEST_METHOD': 'POST', 
'RUN_MAIN': 'true', 
'SCRIPT_NAME': u'', 
'SERVER_NAME': 'localhost.localdomain', 
'SERVER_PORT': '8000', 
'SERVER_PROTOCOL': 'HTTP/1.1', 
'SERVER_SOFTWARE': 'WSGIServer/0.1 Python/2.7.3', 
'SESSION_MANAGER': 'local/unix:@/tmp/.ICE-unix/1264,unix/unix:/tmp/.ICE-unix/1264', 
'SHELL': '/bin/bash', 
'SHLVL': '2', 
'SSH_AUTH_SOCK': '/run/user/1000/keyring-wmgVNG/ssh', 
'TERM': 'xterm-256color', 
'TZ': 'America/Denver', 
'USER': 'username', 
'USERNAME': 'username', 
'WINDOWID': '35702123', 
'WINDOWPATH': '1', 
'XAUTHORITY': '/var/run/gdm/auth-for-username-jI22V1/database', 
'XDG_MENU_PREFIX': 'gnome-', 
'XDG_RUNTIME_DIR': '/run/user/1000', 
'XDG_SEAT': 'seat0', 
'XDG_SESSION_ID': '2', 
'XDG_VTNR': '1', 
'XMODIFIERS': '@im=ibus', 
'_': '/usr/bin/python', 
'wsgi.errors': <open file '<stderr>', mode 'w' at 0x7f9dba515270>, 
'wsgi.file_wrapper': <class wsgiref.util.FileWrapper at 0x2b7fe20>, 
'wsgi.input': <socket._fileobject object at 0x7f9da84e6250>, 
'wsgi.multiprocess': False, 
'wsgi.multithread': True, 
'wsgi.run_once': False, 
'wsgi.url_scheme': 'http', 
'wsgi.version': (1, 0)}> 
################################# 

而且,這裏是我的urls.py文件在我的應用程序

urlpatterns = patterns('', 
url(r'^$',SomeView.as_view()), 
) 

和全球urls.py文件

urlpatterns = patterns('', 
    url(r'^blog/', include('blog.urls')), 
) 

另外,我加入調用打印kwargs和args,並獲得

################################# 
<QueryDict: {}> 
{} 
() 
################################# 
+1

試試'self.request.POST' – karthikr

+0

這仍然返回相同的東西 – user1876508

+0

你是否重定向?你是如何路由到/博客/ – Nix

回答

0

一些更正這至少應該點你在正確的方向:

  1. 當使用基於類的意見請求參數是通過self.request
  2. 使用--data '{"hello":"world"}'訪問,來自卷邊手冊頁:

    -d, --data <data> 
         (HTTP) Sends the specified data in a POST request to the HTTP server, in the 
         same way that a browser does when a user has filled in an HTML form and 
         presses the submit button. This will cause curl to pass the data to the 
         server using the content-type application/x-www-form-urlencoded. Compare to 
         -F, --form. 
    
         -d, --data is the same as --data-ascii. To post data purely binary, you 
         should instead use the --data-binary option. To URL-encode the value of a 
         form field you may use --data-urlencode. 
    
         If any of these options is used more than once on the same command line, the 
         data pieces specified will be merged together with a separating &-symbol. 
         Thus, using '-d name=daniel -d skill=lousy' would generate a post chunk that 
         looks like 'name=daniel&skill=lousy'. 
    
         If you start the data with the letter @, the rest should be a file name to 
         read the data from, or - if you want curl to read the data from stdin. The 
         contents of the file must already be URL-encoded. Multiple files can also be 
         specified. Posting data from a file named 'foobar' would thus be done with 
         --data @foobar. 
    
+0

我添加了self.request方法的輸出,並使用了Advanced REST客戶端來確保我沒有搞砸我的發佈請求。 – user1876508

1

request.POST屬性用於表單編碼數據。您正在發佈json編碼數據,因此請使用request.BODY

欲瞭解更多信息,請參閱http request objects上的文檔。

最後,args和kwargs與過去的數據無關。它們是空的,因爲你沒有在你的url中匹配任何模式。

相關問題