Answer the question
In order to leave comments, you need to log in
Django how to do a simple attribute search?
View:
def search_foto(request):
title = 'Поиск'
try:
query = request.GET['query']
posts = OurFoto.objects.filter(name__startswith=query) | \
OurFoto.objects.filter(text__startswith=query) or \
OurFoto.objects.filter(date_added__startswith=query)
context = {'title': title, 'posts': posts, 'query': query}
return render(request, 'repository_our_fotos/search_form.html', context)
except KeyError:
return render(request, 'repository_our_fotos/search_form.html')
Answer the question
In order to leave comments, you need to log in
You have the wrong code. There is also excellent native Django documentation, a lot of additional materials, books. Why trial and error?
To write correctly:
OurFoto.objects.filter(Q(name__startswith=query) | Q(text__startswith=query) |
Q(date_added__startswith=query))
https://docs.djangoproject.com/en/1.11/topics/db/q...
For queries with the OR operator, you need to use Q object.
from django.db.models import Q
posts = OurFoto.objects.filter(
Q(name__startswith=query) | Q(text__startswith=query) | Q(date_added__startswith=query)
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question