重定向到结果页面时出现 NoReverseMatch 错误

发布于 2024-11-27 15:02:54 字数 1247 浏览 2 评论 0原文

我打算用 Django 编写一个搜索应用程序。
当我重定向到结果页面时,出现 NoReverseMatch 错误。
我在这里发布了我的代码 http://pastebin.com/AL4rG9NU

或者您可以在

网址下面阅读它。 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 = search_files(query)
    return HttpResponseRedirect(reverse('pylucene.views.results', args=(search_results,))) 

错误:

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 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'^

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 = 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.

, 'index'), (r'^search/

views.py


The Error:



I read several similar topics but I can not solve my problem.
Please help me.
Thank you so much.

, 'search'), )

views.py

The Error:

I read several similar topics but I can not solve my problem.
Please help me.
Thank you so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳根太软 2024-12-04 15:02:54

我认为您不理解 reverse 函数是如何工作的,并且您正在尝试什么是不可能的。

对于反向函数,您的 url 必须在 urls.py 上声明,例如:

#urls.py:
urlpatterns = patterns('blog.views',
    (r'^

现在在您的代码中您可以执行以下操作

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

,这就是反向的工作原理。


真正的答案

我认为你不需要 2 个视图,但如果你真的想要:你必须这样做来传递所有已经执行的查询,

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

但我认为你能做的最好的就是这样(使用 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) 
, 'index'), url(r'^blog/(?P<slug>\d{4})/

现在在您的代码中您可以执行以下操作


,这就是反向的工作原理。


真正的答案

我认为你不需要 2 个视图,但如果你真的想要:你必须这样做来传递所有已经执行的查询,


但我认为你能做的最好的就是这样(使用 GET):


, 'blog', name="blog-detail"),
)

现在在您的代码中您可以执行以下操作

,这就是反向的工作原理。


真正的答案

我认为你不需要 2 个视图,但如果你真的想要:你必须这样做来传递所有已经执行的查询,

但我认为你能做的最好的就是这样(使用 GET):

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'^

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) 
, 'index'), url(r'^blog/(?P<slug>\d{4})/

Now in your code you can do


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


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


, 'blog', name="blog-detail"),
)

Now in your code you can do

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文