Django CMS 烦人的错误
我在 Django 项目中有一个简单的搜索应用程序,可以在 CMS 管理员中进行搜索。这是生成 URL 的形式:
< form method="get" action="/search"> < p>< label for="id_q">Search: < input type="text" name="q" id="id_q" /> < input type="submit" value="Submit" />< /p>, this is the URL:
(r'^search/$', 'search.views.search'),this is the view:
def search(request): query = request.GET['q'] results = FlatPage.objects.filter(content__icontains=query) template = loader.get_template('search/search.html') context = Context({ 'query': query, 'results': results }) response = template.render(context) return HttpResponse(response), this is the template:
< html> < head> < title>Search page < /head> < body> < p>You searched for "{{ query }}"; the results are listed below.< /p> < ul> {% for page in results %} < li>< a href="{{ page.get_absolute_url }}">{{ page.title }}< /a>< /li> {% endfor %} < /ul> < /body> < /html>but I keep receiving this error :
"Key 'q' not found in < QueryDict: {} >". Does anyone why and what can I do?
I have a simple search app within a Django project which searches within a CMS admin. This is the form that generates the URL:
< form method="get" action="/search"> < p>< label for="id_q">Search: < input type="text" name="q" id="id_q" /> < input type="submit" value="Submit" />< /p>
, this is the URL:
(r'^search/this is the view:
def search(request): query = request.GET['q'] results = FlatPage.objects.filter(content__icontains=query) template = loader.get_template('search/search.html') context = Context({ 'query': query, 'results': results }) response = template.render(context) return HttpResponse(response), this is the template:
< html> < head> < title>Search page < /head> < body> < p>You searched for "{{ query }}"; the results are listed below.< /p> < ul> {% for page in results %} < li>< a href="{{ page.get_absolute_url }}">{{ page.title }}< /a>< /li> {% endfor %} < /ul> < /body> < /html>but I keep receiving this error :
"Key 'q' not found in < QueryDict: {} >". Does anyone why and what can I do?
, 'search.views.search'),
this is the view:
, this is the template:
but I keep receiving this error :
. Does anyone why and what can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据推测,当您在提交搜索词之前首次请求该页面时,就会发生此错误 - 显然,在请求中找不到
q
字段。您只需要先检查一下:Presumably this error is happening when you first request the page, before you submit a search term - so obviously, the
q
field is not found in the request. You just need to check for it first:当您在提交中发布结果时,您正在使用 request.GET。使用 Daniel 提到的检查并使用 POST 代替:
You are using request.GET when you are posting the results in the submit. Use both the check mentioned by Daniel AND use POST instead:
您可以将该行从
query = request.GET['q']
更改为query = request.GET.get('q')
请注意
get( )
函数...当然这里的其他答案也可以。请参阅类似的问题:'instancemethod'对象没有属性'__getitem__'
You can change the line from
query = request.GET['q']
toquery = request.GET.get('q')
Note the
get()
function... Of course the other answers here also would work. See this similar question:'instancemethod' object has no attribute '__getitem__'