2

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:

Page not found (404)

Request Method:GET

Request URL:http://xxx.x.x.x:8000/search/csrfmiddlewaretoken=W6n1O1vQCMyDojxEkR4mPnRrVz9lYVt1&q=one

No FlatPage matches the given query.

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

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:

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

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:

<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>
Robin
  • 5,366
  • 17
  • 57
  • 87

1 Answers1

2

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

Just like in the documentation

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

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

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • Ok, now i have the html, but even though i have the file, it says `You searched for "one"; the results are listed below.` `No results.` – Robin Jul 30 '13 at 17:13
  • why dont you `render` rather than returning `HttpResponse` – karthikr Jul 30 '13 at 17:20
  • You have a typo. You are sending `resuts` in the context, and doing `{{results}}` in the HTML – karthikr Jul 30 '13 at 17:35
  • Ok, i have mispelled it with 'resuls' and not 'results'. But your answer did solved the main problem. Thanks! – Robin Jul 30 '13 at 17:38