它似乎现在正在运作,我改变的只是在我的模型中对charfield的图表。为什么不用chartext搜索呢?那是修复吗?
我在djangobook的第7部分遇到了一些问题,我们在这里展示了我们图书搜索的结果。我不是在使用书籍,但我不知道我做错了什么。
注意:搜索字词“q”正在正确传递,它只是不会显示结果。模板确实出现了。我打算跳过它,但我真的开始想知道我还有什么不对的?
我关注的是:http://djangobook.com/en/2.0/chapter07/
这是我的代码
urls.py
from django.conf.urls.defaults import patterns, include, url
from EM.catalog import views
urlpatterns = patterns('',
(r'^search-form/$', views.search_form),
(r'^search/$', views.search),
views.py
from django.template import loader, Context
from django.shortcuts import render_to_response
from django.http import HttpResponse
from EM.catalog.models import em_tank
def search_form(request):
return render_to_response('search_form.html')
def search(request):
if 'q' in request.GET and request.GET['q']:
q = request.GET['q']
tanknotes = em_tank.objects.filter(em_tank_notes__icontains=q)
return render_to_response('search-results.html', {'tanknotes': tanknotes, 'query': q})
else:
return HttpResponse("Please input a search term")
搜索results.html
<p>You searched for: <strong>{{ query }}</strong></p>
{% if tanknotes %}
<p>Found {{ tanknotes|length }} result{{ tanknotes|pluralize }}.</p>
<ul>
{% for em_tank_notes in tanknotes %}
<li>{{ em_tank.em_tank_notes }}</li>
{% endfor %}
</ul>
{% else %}
<p>No tanks matched your search criteria.</p>
{% endif %}
模型
from django.db import models
class em_tank(models.Model):
em_tank_date_acquired = models.DateField()
em_tank_notes = models.CharField(max_length=150, verbose_name='Notes', blank=True)