django搜索页面未找到错误 [英] django search page not found error

查看:75
本文介绍了django搜索页面未找到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在应该能够在页面上键入搜索查询的地步,而django应该返回匹配页面的列表(如果有的话).但是它没有显示任何页面,即使它在那里,也给了我一个错误.假设我有一个页面的内容是一页,当我搜索时,出现此错误:

I am stuck at a point where i should be able to type in a search query on the page and it django should get back a list of matching pages if any. But it doesnt show me any pages, even though its there, and gives me an erros. Suppose I have one page whose content is one, when i search, i get this error:

找不到页面(404)

Page not found (404)

请求方法:GET

请求网址: http://xxx.xxx:8000/search/csrfmiddlewaretoken= W6n1O1vQCMyDojxEkR4mPnRrVz9lYVt1& q = one

没有FlatPage匹配给定查询.

No FlatPage matches the given query.

请指出我做错了什么.谢谢.

Please point me as to where am doing wrong. Thank you.

我的views.py:

my views.py:

from django.http import HttpResponse
from django.template import loader, Context
from django.contrib.flatpages.models import FlatPage

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

urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    (r'^tinymce/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': 'C:/Users/Kakar/web/cms/static/js/tinymce/' }),
    (r'', include('django.contrib.flatpages.urls')),
    (r'^search/$', 'search.views.search'),
)

settings.py:

settings.py:

在已安装的应用程序中,我已经安装了搜索".

In the installed-apps, I have installed 'search'.

default.html:

default.html:

<html>
    <head>
        <title>{{ flatpage.title }}</title>
    </head>
    <body>
        <form method="get" action="/search/">
        {% csrf_token %}
            <p><label for="id_q">Search:</label>
            <input type="text" name="q" id="id_q" />
            <input type="submit" value="Submit" /></p>
        </form>
        <h1>{{ flatpage.title }}</h1>
        {{ flatpage.content }}
    </body>
</html>

search.html:

search.html:

<html>
    <head>
        <title>Search</title>
    </head>
    <body>
        <p>You searched for "{{query}}"; the results are listed below.</p>
        {% if results %}
            <ul>
                {% for page in results %}
                    <li>
                        <a href="{{page.get_absolute_url}}">{{page.title}}</a>
                    </li>
                {% endfor %}
            </ul>
        {% else %}
            <p>No results.</p>
        {% endif %}
    </body>
</html>

推荐答案

尝试更改URL匹配的顺序.

Try changing the order of the URL matches.

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    (r'^tinymce/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': 'C:/Users/Kakar/web/cms/static/js/tinymce/' }),
    (r'^search/$', 'search.views.search'),
    (r'', include('django.contrib.flatpages.urls')),
)

就像文档

此外,由于某种原因,您在查询参数中缺少?.

Also, for some reason, you are missing the ? in the query parameters.

另一个问题:您有错字.您正在上下文中发送 resuts ,并在HTML中执行 {{results}}

One more issue: You have a typo. You are sending resuts in the context, and doing {{results}} in the HTML

这篇关于django搜索页面未找到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆