couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ian Mulvany" <>
Subject couchdb-python: failing to 'PUT' document views
Date Tue, 20 May 2008 18:46:51 GMT

I'd like to use the couchdb python API to insert views, but I am
failing to understand the
difference between a view and a document.

I've installed couchdb on max osx 10.5

I followed the very nice instructions from

and using the documentation with the provided python api we have:

import httplib, simplejson
def prettyPrint(s):
    """Prettyprints the json response of an HTTPResponse object"""
    # HTTPResponse instance -> Python object -> str
    print simplejson.dumps(simplejson.loads(, sort_keys=True, indent=4)

class Couch:
    """Basic wrapper class for operations on a couchDB"""
    def __init__(self, host, port=5984, options=None): = host
        self.port = port
    def connect(self):
        return httplib.HTTPConnection(, self.port) # No close()
    def createDb(self, dbName):
        """Creates a new database on the server"""
        r = self.put(''.join(['/',dbName,'/']), "")

    def saveDoc(self, dbName, body, docId=None):
        """Save/create a document to/in a given database"""
        if docId:
            print ''.join(['/', dbName, '/', docId])
            r = self.put(''.join(['/', dbName, '/', docId]), body)
            r =''.join(['/', dbName, '/']), body)
    def put(self, uri, body):
        c = self.connect()
        if len(body) > 0:
            headers = {"Content-type": "application/json"}
            c.request("PUT", uri, body, headers)
            c.request("PUT", uri, body)
        return c.getresponse()

# test creation of db and addition of doc
foo = Couch('localhost', '5984')
doc = """
            "Subject":"I like Planktion",
            "Tags":["plankton", "baseball", "decisions"],
            "Body":"I decided today that I don't like baseball. I like
foo.saveDoc('testblog', doc, 'mypost')
# this works, but now:

view = """
  "language": "text/javascript",  # also breaks when I omit this line
    "all": "function(doc) { if (doc.type == 'post')  map(null, doc) }",
    "by_tags": "function(doc) { if (doc.type == 'post')  map(doc.Tags, doc) }"
foo.saveDoc('testblog', view, '_design/addview2')

# does not work but provides the following error:ValueError: No JSON
object could be decoded

Any advice would be greatly appreciated,

- Ian

View raw message