基于Django类的视图正在返回一个空的POST字典

基于Django类的视图正在返回一个空的POST字典,django,rest,post,django-class-based-views,Django,Rest,Post,Django Class Based Views,我有一个django视图,定义如下 class SomeView(View): def post(self, request, *args, **kwargs): print "###############################" print request.POST print "###############################" return HttpResponse(json.dumps(k

我有一个django视图,定义如下

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: {}>
###############################
当我调用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)}>
#################################
全局URL.py文件是

urlpatterns = patterns('',
    url(r'^blog/', include('blog.urls')),
)
此外,我还添加了打印kwargs和args的调用,并获取

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

一些更正至少可以为您指明正确的方向:

  • 使用基于类的视图时,可通过
    self.request
  • 使用curl手册页中的
    --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.
    
    -d,--data
    (HTTP)将POST请求中的指定数据发送到
    与浏览器在用户填写HTML表单和
    按下提交按钮。这将导致curl将数据传递给
    使用内容类型application/x-www-form-urlencoded的服务器。比照
    -F、 --形式。
    -d、 --数据与--数据ascii相同。要发布纯二进制数据,您需要
    应该改为使用--data二进制选项。对
    可以使用的表单字段--data urlencode。
    如果在同一命令行上多次使用这些选项中的任何一个,则
    指定的数据段将与分隔的&-符号合并在一起。
    因此,使用'-d name=daniel-d skill=loous'将生成
    看起来像‘name=daniel&skill=loosy’。
    如果数据以@开头,那么其余的应该是要删除的文件名
    从中读取数据,或者-如果您想从stdin中读取数据。这个
    文件的内容必须已经是URL编码的。还可以创建多个文件
    明确规定。因此,从名为“foobar”的文件发布数据将通过
    --数据@foobar。
    

  • request.POST
    属性用于表单编码数据。您正在发布json编码的数据,因此请改用
    request.BODY

    有关更多信息,请参阅上的文档


    最后,args和kwargs与过去的数据无关。它们是空的,因为您没有匹配url中的任何模式

    尝试一下仍然返回相同内容的
    self.request.POST
    ,您是否以某种方式重定向?你是如何发送到/blog/I的,上面已经发布了代码。这可能是其他地方的问题吗?@Nix让我添加我的url.py文件。另外,我发布了self.request数据。我添加了self.request方法的输出,并使用了高级REST客户端来确保我没有破坏我的post请求。
    #################################
    <QueryDict: {}>
    {}
    ()
    #################################
    
       -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.