title: Django1.8 Class-based-view for authenticated file uploads
body: <p>Am new to Django and trying to write a HTTP server that accepts file uploads via POST only after authentication. Quite a few tutorials suggested CBV over function based views but when I try to do a POST, following error is thrown. Can anyone please help with what am doing wrong?</p>
  
  <pre><code>Starting server, use &lt;Ctrl-C&gt; to stop.
  ----------------------------------------
  Exception happened during processing of request from ('10.1.100.2', 30338)
  Traceback (most recent call last):
    File "/usr/lib/python2.7/SocketServer.py", line 295, in
    _handle_request_noblock
      self.process_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
      self.finish_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
      self.RequestHandlerClass(request, client_address, self)
  TypeError: __init__() takes exactly 1 argument (4 given)
  ----------------------------------------
  ----------------------------------------
  Exception happened during processing of request from ('10.1.100.2', 30339)
  Traceback (most recent call last):
    File "/usr/lib/python2.7/SocketServer.py", line 295, in 
    _handle_request_noblock
      self.process_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
      self.finish_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
      self.RequestHandlerClass(request, client_address, self)
  TypeError: __init__() takes exactly 1 argument (4 given)
  ----------------------------------------
  ----------------------------------------
  Exception happened during processing of request from ('10.1.100.2', 30340)
  Traceback (most recent call last):
    File "/usr/lib/python2.7/SocketServer.py", line 295, in 
    _handle_request_noblock
      self.process_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
      self.finish_request(request, client_address)
    File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
      self.RequestHandlerClass(request, client_address, self)
  TypeError: __init__() takes exactly 1 argument (4 given)
  ----------------------------------------
  </code></pre>
  
  <p>urls.py:</p>
  
  <pre><code>from django.conf.urls import include, url
  from django.contrib import admin
  import my_app.views
  admin.autodiscover()
  
  urlpatterns = [
      url(r'^admin/', include(admin.site.urls)),
      url(r'^post/', my_app.views.Handler.as_view(), name='my_app'),
  ]
  </code></pre>
  
  <p>views.py:</p>
  
  <pre><code>from django.shortcuts import render
  from django.http import HttpResponse
  from django.shortcuts import render_to_response
  from django.template import Context, loader
  from django.http import Http404
  from django.views.generic.base import View
  from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  import cgi
  import urlparse
  import datetime
  import SimpleHTTPServer
  import SocketServer
  
  class Handler(View, BaseHTTPRequestHandler):
      def do_HEAD(self):
          print "send header"
          self.send_response(200)
          self.send_header('Content-type', 'text/html')
          self.end_headers()
  
      def do_AUTHHEAD(self):
          print "send header"
          self.send_response(401)
          self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
          self.send_header('Content-type', 'text/html')
          self.end_headers()
  
      def do_GET(self):
          if self.headers.getheader('Authorization') == None:
              self.do_AUTHHEAD()
              self.wfile.write('no auth header received')
              pass
          elif self.headers.getheader('Authorization') == 'Basic dGVzdDp0ZXN0':
              self.do_HEAD()
              self.wfile.write(self.headers.getheader('Authorization'))
              self.wfile.write('authenticated!')
              parsed_path = urlparse.urlparse(self.path)
              message_parts = [
                  'CLIENT VALUES:',
                  'client_address=%s (%s)' % (self.client_address,
                                              self.address_string()),
                  'command=%s' % self.command,
                  'path=%s' % self.path,
                  'real path=%s' % parsed_path.path,
                  'query=%s' % parsed_path.query,
                  'request_version=%s' % self.request_version,
                  '',
                  'SERVER VALUES:',
                  'server_version=%s' % self.server_version,
                  'sys_version=%s' % self.sys_version,
                  'protocol_version=%s' % self.protocol_version,
                  '',
                  'HEADERS RECEIVED:',
                  ]
              for name, value in sorted(self.headers.items()):
                  message_parts.append('%s=%s' % (name, value.rstrip()))
              message_parts.append('')
              message = '\r\n'.join(message_parts)
              self.send_response(200)
              self.end_headers()
              self.wfile.write(message)
              pass
          else:
              self.do_AUTHHEAD()
              self.wfile.write(self.headers.getheader('Authorization'))
              self.wfile.write('not authenticated')
              pass
  
  
      def do_POST(self):
          form = cgi.FieldStorage(
              fp=self.rfile,
              headers=self.headers,
              environ={'REQUEST_METHOD':'POST',
                       'CONTENT_TYPE':self.headers['Content-Type'],
                       })
  
          self.send_response(200)
          self.end_headers()
          self.wfile.write('Client: %s\n' % str(self.client_address))
          self.wfile.write('User-agent: %s\n' % str(self.headers['user-agent']))
          self.wfile.write('Path: %s\n' % self.path)
          self.wfile.write('Form data:\n')
  
          for field in form.keys():
              field_item = form[field]
              if field_item.filename:
                  # The field contains an uploaded file
                  file_data = field_item.file.read()
                  file_len = len(file_data)
                  del file_data
                  self.wfile.write('\tUploaded %s as "%s" (%d bytes)\n' % \
                          (field, field_item.filename, file_len))
              else:
                  self.wfile.write('\t%s=%s\n' % (field, form[field].value))
          pass
  server = HTTPServer(('', 8080), Handler)
  print 'Starting server, use &lt;Ctrl-C&gt; to stop.'
  server.serve_forever()
  </code></pre>