Django CMS 烦人的错误

发布于 2024-11-28 08:41:39 字数 1324 浏览 7 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(3

北风几吹夏 2024-12-05 08:41:39

据推测,当您在提交搜索词之前首次请求该页面时,就会发生此错误 - 显然,在请求中找不到 q 字段。您只需要先检查一下:

def search(request):
    if 'q' in request.GET:
        query = request.GET['q']
        results = FlatPage.objects.filter(content__icontains=query)

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:

def search(request):
    if 'q' in request.GET:
        query = request.GET['q']
        results = FlatPage.objects.filter(content__icontains=query)
明月松间行 2024-12-05 08:41:39

当您在提交中发布结果时,您正在使用 request.GET。使用 Daniel 提到的检查并使用 POST 代替:

def search(request):
    if 'q' in request.GET:
      query = request.GET['q']
      results = FlatPage.objects.filter(content__icontains=query)
    else:
      query = ""
      results = None
    template = loader.get_template('search/search.html')
    context = Context({ 'query': query, 'results': results })
    response = template.render(context)
    return HttpResponse(response)        

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:

def search(request):
    if 'q' in request.GET:
      query = request.GET['q']
      results = FlatPage.objects.filter(content__icontains=query)
    else:
      query = ""
      results = None
    template = loader.get_template('search/search.html')
    context = Context({ 'query': query, 'results': results })
    response = template.render(context)
    return HttpResponse(response)        
东风软 2024-12-05 08:41:39

您可以将该行从 query = request.GET['q'] 更改为 query = request.GET.get('q')

请注意 get( ) 函数...当然这里的其他答案也可以。请参阅类似的问题:

'instancemethod'对象没有属性'__getitem__'

You can change the line from query = request.GET['q'] to query = 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__'

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