开发者

NoReverseMatch error while redirecting to results page

I have intended to write an search application by Django.

I am getting a NoReverseMatch error when I redirect to results page.

I posted my codes here http://pastebin.com/AL4rG9NU

Or you can read it below

urls.py

urlpatterns = patterns('pylucene.views',
    (r'^$', 'index'),
    (r'^search/$', 'search'),
)

views.py

def index(request):
     return render_to_response('pylucene/index.html', context_instance=RequestContext(request))


def search(request):
    query = request.POST['query']
    search_results开发者_如何学JAVA = search_files(query)
    return HttpResponseRedirect(reverse('pylucene.views.results', args=(search_results,))) 

The Error:

NoReverseMatch at /pylucene/search/
 Reverse for 'pylucene.views.results' with arguments
 '([(u'Documents\\Dieu khien may tinh bang y nghi.txt', u'Dieu khien
 may tinh bang y nghi.txt'), '1 total matching documents.'],)' and
 keyword arguments '{}' not found.
def results(request, search_results):
return render_to_response('pylucene/results.html', {'search_results': search_results}, context_instance=RequestContext(request))

I read several similar topics but I can not solve my problem. Please help me.

Thank you so much.


I think that you are not undestanding how the reverse function works and what are you trying is just not posible.

For the reverse function your url must be declared on urls.py for example:

#urls.py:
urlpatterns = patterns('blog.views',
    (r'^$', 'index'),
    url(r'^blog/(?P<slug>\d{4})/$', 'blog', name="blog-detail"),
)

Now in your code you can do

reverse('blog-detail', args=('django-urls',))
# the resolt of this is
# /blog/django-urls/

And this is how reverse works.


The real answer

I think that you do not need 2 views, but if you really want to: you have to do this to pass all the query already performed

def search(request):
    query = request.POST['query']
    search_results = search_files(query)
    return results(request, search_results)

but i think that the best that you can do is this (using GET):

def search(request):
    query = request.GET['query']  # I think using get is better because you are GETing info
    search_results = search_files(query) # better if returns a generator
    return render_to_response('pylucene/results.html',
                              {'search_results': search_results},
                              context_instance=RequestContext(request) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜