首页 > Django -- Cannot resolve keyword 'title_icontains' into field

Django -- Cannot resolve keyword 'title_icontains' into field

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

icontains是不区分大小写模糊匹配,查询时用 字段__icontains=值 中间是两个下划线,楼上正解


你去看看models部分,应该是title__icontains,注意是__,而不是_

【热门文章】
【热门文章】