python - Django -- Cannot resolve keyword 'title_icontains' into field
本文介绍了python - 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
解决方案
你去看看models部分,应该是title__icontains
,注意是__
,而不是_
这篇关于python - Django -- Cannot resolve keyword 'title_icontains' into field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文