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
巴扎黑2017-04-17 17:44:53
icontains는 대소문자를 구분하지 않는 퍼지 일치입니다. 쿼리할 때 중간에 두 개의 밑줄이 있는 __icontains=value 필드를 사용하세요.