Reputation: 1
I want to make search function in my project, with Model Agreements
from django.db import models
class Agreements(models.Model):
username = models.CharField(max_length=128) # this i want to use to search
email = models.CharField(max_length=128, unique=True)
phone_number = models.CharField(max_length=128, unique=True)
creation_date = models.DateField()
conclusion_date = models.DateField()
def __str__(self):
return str(self.username)
views.py
from django.shortcuts import render
from django.views.generic import ListView
from django.db.models import Q
from connections.models import Agreements, Connections
def index(request):
context = {
'index': Agreements.objects.all(),
'HM': Connections.objects.all(),
}
return render(request, 'list_agreements.html', context)
class Search(ListView):
template_name = 'list_agreements.html'
context_object_name = 'Agreements'
def get_queryset(self):
return Agreements.objects.filter(username__icontains=self.request.GET.get('q'))
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['q'] = self.request.GET.get('q')
return context
my urls.py
from django.contrib import admin
from django.urls import path
from connections.views import index, Search
urlpatterns = [
path('admin/', admin.site.urls),
path('', index, name='index'),
path('search/', Search.as_view(), name='search'),
]
[enter image descrienter image description hereption here](https://i.sstatic.net/gClVN.png)
my list_agreements.html (only important parts)
<form class="d-flex" role="search" action="{% url 'search' %} " method="get">
<input class="form-control me-2" type="search" type="text" placeholder="Search" aria-label="Search" name="q">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
<div class="limiter">
<div class="container-table100">
<div class="wrap-table100">
<div class="table100 ver1">
<div class="table100-firstcol">
</tbody>
</table>
</div>
<div class="wrap-table100-nextcols js-pscroll">
<div class="table100-nextcols">
<table>
<thead>
<tr class="row100 head">
<th class="cell100 column2">ФИО</th>
<th class="cell100 column3">Почта</th>
<th class="cell100 column4">Номер телефона</th>
<th class="cell100 column5">Создания договора</th>
<th class="cell100 column6">Заключение договора</th>
</tr>
</thead>
{% for Agreements in index %}
<tbody>
<tr class="row100 body">
<td class="cell100 column2">{{ Agreements.username }}</td>
<td class="cell100 column3">{{ Agreements.email }}</td>
<td class="cell100 column4">{{ Agreements.phone_number }}</td>
<td class="cell100 column5">{{ Agreements.creation_date }}</td>
<td class="cell100 column6">{{ Agreements.conclusion_date }}</td>
{% endfor %}
I execpted to see in ФИО username Иван Градинар Дмитривич, but i get empty list
Upvotes: 0
Views: 32
Reputation: 46
<form class="d-flex" role="search" action="{% url 'search' %} " method="get">
<input class="form-control me-2" type="search" type="text" placeholder="Search" aria-label="Search" name="q">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
<div class="limiter">
<div class="container-table100">
<div class="wrap-table100">
<div class="table100 ver1">
<div class="table100-firstcol">
</tbody>
</table>
</div>
<div class="wrap-table100-nextcols js-pscroll">
<div class="table100-nextcols">
<table>
<thead>
<tr class="row100 head">
<th class="cell100 column2">ФИО</th>
<th class="cell100 column3">Почта</th>
<th class="cell100 column4">Номер телефона</th>
<th class="cell100 column5">Создания договора</th>
<th class="cell100 column6">Заключение договора</th>
</tr>
</thead>
<tbody>
{% for Agreements in index %}
<tr class="row100 body">
<td class="cell100 column2">{{ Agreements.username }}</td>
<td class="cell100 column3">{{ Agreements.email }}</td>
<td class="cell100 column4">{{ Agreements.phone_number }}</td>
<td class="cell100 column5">{{ Agreements.creation_date }}</td>
<td class="cell100 column6">{{ Agreements.conclusion_date }}</td>
</tr>
{% endfor %}
{% for Agreements in object_list %}
<tr class="row100 body">
<td class="cell100 column2">{{ Agreements.username }}</td>
<td class="cell100 column3">{{ Agreements.email }}</td>
<td class="cell100 column4">{{ Agreements.phone_number }}</td>
<td class="cell100 column5">{{ Agreements.creation_date }}</td>
<td class="cell100 column6">{{ Agreements.conclusion_date }}</td>
</tr>
{% endfor %}
</tbody>
Here object_list contains the list of objects returned by your search view just like your index. get method adds object list to the context.Append the code in list_agreements.html.
Upvotes: 1