python - Django -- Cannot resolve keyword 'title_icontains' into field

 握住每一缕光 发布于 2022-10-30 09:20
from django.shortcuts import render
from django.db.models import Q
from models import Book
from django.shortcuts import render_to_response
from django import forms
from forms import ContactForm
# Create your views here.

def search(request):
    query = request.GET.get('q','')
    if query:
        qset=(
            Q(title_icontains=query) |
            Q(authors_first_name_icontains=query) |
            Q(authors_last_name_icontains=query)
            )
        results=Book.objects.filter(qset).distinct()
    else:
        results=[]
    return render_to_response(r"search.html",{
        "results":results,
        "query":query
        })

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
    else:
        form=ContactForm()
    return render_to_response('contact.html',{'form':form})


TOPIC_CHOICES = (
    ('general','General enquiry'),
    ('bug','Bug report'),
    ('suggestion','Suggestion'),
    )

class ContactForm(forms.Form):
    topic = forms.ChoiceField(choices=TOPIC_CHOICES)
    message = forms.CharField()
    sender = forms.EmailField(required=False)

ERROR

FieldError at /search/
Cannot resolve keyword 'title_icontains' into field. Choices are: authors, id, num_pages, publication_date, publisher, publisher_id, title
Request Method:    GET
Request URL:    http://127.0.0.1:8000/search/?q=authors
Django Version:    1.8.6
Exception Type:    FieldError
Exception Value:    
Cannot resolve keyword 'title_icontains' into field. Choices are: authors, id, num_pages, publication_date, publisher, publisher_id, title
Exception Location:    C:\Python27\lib\site-packages\django-1.8.6-py2.7.egg\django\db\models\sql\query.py in names_to_path, line 1397
Python Executable:    C:\Python27\python.exe
Python Version:    2.7.11
Python Path:    
['D:\\Users\\rongweiwei799\\mysite',
 'C:\\Python27\\lib\\site-packages\\python_docx-0.8.5-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\selenium-2.53.1-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\django-1.8.6-py2.7.egg',
 'C:\\Python27\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages']
Server time:    Thu, 5 May 2016 18:25:22 +0800
2 个回答
  • icontains是不区分大小写模糊匹配,查询时用 字段__icontains=值 中间是两个下划线,楼上正解

    2022-10-31 21:20 回答
  • 你去看看models部分,应该是title__icontains,注意是__,而不是_

    2022-10-31 21:20 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有