I am trying to make the search bar work. Whenever I type something in views.py
, it should be appeared and others shouldn't. I have written code which I will show but when I run the code, it gives me attribute error type object 'Destination' has no attribute 'filter'
. How to solve this problem?
index.html
<form class="love" method="GET" action="">
<input type="text" placeholder='Search..' name="srh" value="{{request.GET.srh}}"> <br>
<button type="submit" class="btn btn-danger"> Search </button>
</form>
views.py
from django.shortcuts import render
from . models import Destination
from django.db.models import Q
def index(request):
query = request.GET.get('srh')
if query:
match = Destination.filter(Q(desc_icontains=query))
# instead of writing this
target1 = a, b= [Destination() for __ in range(2)]
a.img = 'Article.jpg'
b.img = 'Micro Tasks.jpeg'
a.desc = 'Article Writing'
b.desc = 'Micro Tasks'
# I am trying to make a loop but it is not working.
target1 = Destination.objects.all()
for field in target1:
[Destination(img = f'{field.img}', title = f'{field.title}') for __ in range(2)]
app url
from . import views
urlpatterns = [path('', views.index, name='index')]
main url
from django.contrib import admin
from django.urls import path, include
from firstapp.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('firstapp.urls'))]
Installed apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Your Destination
class is not a model. In order to make something a model, you should make that class a subclass of the Model
class [Django-doc] . Furthermore the fields you want to store in the database should be instances of the Field
class . You thus should rewrite your model to:
class Destination():
desc =
img =
price =
There is no need to add an id
field. If you do not specify a primary key yourself, Django will make a field named id
that is an AutoField
. You might want to make img
an ImageField
[Django-doc] instead, but that is another discussion.
Once you have constructed that model, you can run manage.py makemigrations
[Django-doc] to make migration files; and manage.py migrate
[Django-doc] to migrate your database and thus create the corresponding table(s).
In order to access the records of a model, you need to access a Manager
, like the .objects
Django will automatically attach to your model. Only Manager
s and QuerySet
s can .filter(..)
, .exclude(..)
, etc.
Another problem that you will encounter is that you need two consecutive underscores to use a field lookup, so __icontains
, instead of _icontains
.
Finally using .distinct()
is useless here, since desc
is a model field on the Destination
model, and thus the filtering will not JOIN with outher tables.
You can thus implement this as:
match = Destination..filter(desc=query)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.