Django的简单的搜索

问题描述:

我通过在那里我应该创建一个简单的搜索表单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' 

这是URL模式:

(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> 

我只是不明白,我可能可以出了错

+0

究竟是什么“错误”?什么不行?你遇到了什么错误?你有什么问题? – 2010-07-14 13:19:16

+0

例外值: 'function'对象没有属性'render' – MacPython 2010-07-14 13:29:16

+0

提示。 (1)发布证明问题的最少代码。 (2)确实,真正有组织地提出你的问题。这是在“太长:没有阅读”的边缘。 – 2010-07-14 13:33:41

template = loader.get_template 

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

template = loader.get_template() 
+0

是的,就是这样!太感谢了! – MacPython 2010-07-14 13:51:56