Django简单搜索表单

时间:2010-07-14 13:15:03

标签: django

我正在运行一个教程,我应该创建一个简单的搜索表单

这是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']
    results = FlatPage.objects.filter(content__icontains=query)
    template = loader.get_template
    context = Context({ 'query': query, 'results':results })
    response = template.render(context)
    return HttpResponse(response)

这是我得到的错误:

Exception Value:   

'function' object has no attribute 'render'

这是URLpattern:

(r'^search/$', 'Mysite.search.views.search'),

这是默认模板:

<html>
 <head>
  <title>{{ flatpage.title }}</title>
 </head>
 <body>
 <form method="get" action="/search/">
  <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>
 <head>
  <title> Search </title>
 </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>

我根本不知道我可能出错的地方

1 个答案:

答案 0 :(得分:3)

template = loader.get_template

您不应该简单地为函数分配新变量。你应该实际评估这个功能。

template = loader.get_template()