【问题标题】:django search page not found errordjango 搜索页面未找到错误
【发布时间】:2013-07-30 21:48:28
【问题描述】:

我被困在一个点,我应该能够在页面上输入搜索查询,如果有的话,django 应该返回匹配页面的列表。但它没有显示任何页面,即使它在那里,并且给了我一个错误。假设我有一页内容为一,当我搜索时,我得到这个错误:

找不到页面 (404)

请求方法:GET

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

没有 FlatPage 匹配给定的查询。

请指出我哪里做错了。谢谢。

我的意见.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:

在已安装的应用程序中,我安装了“搜索”。

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>

搜索.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>

【问题讨论】:

  • 尝试从您的网址格式中删除“$”。即(r'^search/', 'search.views.search')

标签: django django-templates django-views


【解决方案1】:

尝试更改 URL 匹配的顺序。

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')),
)

就像在the documentation中一样

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

还有一个问题:你有一个错字。您在上下文中发送 resuts,并在 HTML 中发送 {{results}}

【讨论】:

  • 好的,现在我有 html,但即使我有文件,它也会显示 You searched for "one"; the results are listed below. No results.
  • 你为什么不render而不是返回HttpResponse
  • 你有一个错字。您在上下文中发送 resuts,并在 HTML 中发送 {{results}}
  • 好吧,我把它拼错了“resuls”而不是“results”。但是您的回答确实解决了主要问题。谢谢!
猜你喜欢
  • 2020-10-28
  • 2017-07-17
  • 1970-01-01
  • 2021-02-01
  • 2016-04-24
  • 2019-11-20
  • 1970-01-01
  • 2015-06-22
  • 2013-12-04
相关资源
最近更新 更多