重定向到结果页面时出现 NoReverseMatch 错误
我打算用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不理解
reverse
函数是如何工作的,并且您正在尝试什么是不可能的。对于反向函数,您的 url 必须在 urls.py 上声明,例如:
现在在您的代码中您可以执行以下操作
,这就是反向的工作原理。
真正的答案
我认为你不需要 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:
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):