gpt4 book ai didi

Django 简单搜索表单

转载 作者:行者123 更新时间:2023-12-04 06:50:11 24 4
gpt4 key购买 nike

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

这是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>

我只是不明白我可能出错的地方

最佳答案

template = loader.get_template

您不应该简单地为函数分配一个新变量。您应该实际评估该功能。
template = loader.get_template()

关于Django 简单搜索表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3246443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com