accessing couchdb stored view using django
I'm using the Django framework with Couchdb, building on the example from here: intro to using couchdb with django
I've had no trouble using ad-hoc couchdb views, but when I replace it with a stored view I'm getting back no results.
This, for example, worked:
def index(request):
docs = SERVER['docs']
all_pages = "function(d) { if ( (d.title.length>0) && (d.type=='otherpage') ) emit(d.title,d); }"
pages = [ x for x in docs.query(all_pages) ]
all_blogposts = "function(d) { if ( (d.title.length>0) && (d.type=='blogpost') ) emit([d.date,d.title],d); }"
blogposts = [ x for x in docs.query(all_blogposts) ]
return render_to_response('couch_docs/index.html',{'pages':pages,'blogposts':blogposts})
However, this did not:
def index(request):
docs = SERVER['docs']
all_pages = docs.view('_view/example/all_pages')
pages = all_pages.rows()
all_blogposts = "function(d) { if ( (d.title.length>0) && (d.type=='blogpost') ) emit([d.date,d.title],d); }"
blogposts = [ x for x in docs.query(all_blogposts) ]
return render_to_response('couch_docs/index.html',{'pages':pages,'blogposts':blogposts})
When I go to the Futon screen and select the example/all_pages view, it returns several documents. However, when I try to use this from within Django it returns no results. It also returns nothing when I do it at the python command line. It would appear that I am not defining all_pages correctly? If I print it out it's the following:
<ViewResults <PermanentView '_v开发者_开发百科iew/example/all_pages'> {}>
So, yeah, it's empty. Can anyone see what I'm doing wrong? When I select the "all_pages" view under "example" in Futon, it comes back with results with no problem. Any help is appreciated.
p.s. so I also tried this tutorial:Snakes on a Couch!
I have the same problem, namely that while I can connect to Couch, start up Futon, create databases, documents, and design documents (at the python command line) and then see them (including in Futon), I cannot get results back from a permanent view. I found again that I could run the view from inside Futon (the exact same one created at the python command line), and get results back, but when trying to use the view from python I got no results back.
Since this was doing character for character what they were doing in the tutorial, I am thinking it probably means there's not a problem with my code, but rather with my software setup. Any ideas on what piece is most likely to have the problem, or how to track it down?
There's a couple different reasons this could be happening. Keep in mind that Futon adds a couple extra parameters automatically, try loading the view directly:
curl http://localhost:5984/docs/_design/example/_view/all_pages
If that does load than it's an issue with your Python code. In python-couchdb the syntax would be:
couchdb.Server('http://localhost:5984')['docs'].view('example/all_pages')
If that works, than it's an issue with how your interacting with your couchdb library.
Incidentally, [x for x in anything] is the same as just using the list constructor, e.g. list(anything).
Well here's what worked:
def index(request):
docs = SERVER['docs']
pages = [ x for x in docs.view("_design/example/_view/all_pages") ]
all_blogposts = "function(d) { if ( (d.title.length>0) && (d.type=='blogpost') ) emit([d.date,d.title],d); }"
blogposts = [ x for x in docs.query(all_blogposts) ]
return render_to_response('couch_docs/index.html',{'pages':pages,'blogposts':blogposts})
I'm not totally sure why that worked, since the examples and documentation I read seemed to indicate that I didn't need the _design and/or the _view in there. But, you know, it works, so I'm going with it. Perhaps it's some version difference in couchdb-python?
精彩评论