장고로 간단한 검색 기능을 만들어 보았습니다.
1. 우선 취급하는 데이터 테이블을 만든다
· 모델
myapp/search/models.pyfrom django.db import models
class People(models.Model):
  name = models.CharField(max_length=20)
  def __str__(self):
      return self.name
· views.py
myapp/search/views.pyfrom django.shortcuts import render
from .models import People
from django.views.generic import ListView
class PeopleList(ListView):
    model = People
주) 여기서 사용하는 template의 이름은 자동으로
people_list.html· urls.py
myapp/search/urls.pyfrom django.urls import path
from . import views
urlpatterns = [
    path('', views.PeopleList.as_view(), name='people'),
]
· 표시 템플릿
myapp/search/templates/search/people_list.html<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>people_list</title>
</head>
<body>
  <table border="1">
    <tr>
        <th>name</th>
    </tr>
    {% for people in object_list %}
    <tr>
        <td>{{ people.name }}</td>
    </tr>
    {% endfor %}
</table>
</body>
</html>
・출력 결과
데이터는 admin 화면에서 적당하게 등록한다

2. 검색 기능 만들기
models.py と urls.pyは変更なし· views.py
myapp/search/views.pyfrom django.shortcuts import render
from .models import People
from django.views.generic import ListView
class PeopleList(ListView):
    def get_queryset(self):
        query = self.request.GET.get('query')
        if query:
            people_list = People.objects.filter(
                name__icontains=query)
        else:
            people_list = People.objects.all()
        return people_list
__contains 는 소문자 대문자 구별__icontains 는 소문자 대문자 구분 없음이것들은 정확히 일치하지 않고 포함 된 것을 얻습니다.
· 표시 템플릿
myapp/search/templates/search/people_list.html<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>people_list</title>
  </head>
  <body>
    <!-- formを追加 -->
    <form action="" method="get">
      <input name="query" value="{{ request.GET.query }}" type="text" />
      <button type="submit">検索する</button>
    </form>
    <table border="1">
      <tr>
        <th>name</th>
      </tr>
      {% for people in object_list %}
      <tr>
        <td>{{ people.name }}</td>
      </tr>
      {% endfor %}
    </table>
  </body>
</html>
request.GET.query 에서 URL 매개 변수를 가져옵니다.즉,
http://127.0.0.1:8000/?query=<検索ワード> 와 같은 파라미터를 얻을 수 있다・출력 결과

단단히 검색 할 수 있습니다.
했어.
이번에 만들었지만 github
참고원
Reference
이 문제에 관하여(장고로 간단한 검색 기능을 만들어 보았습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/haruki00000000/items/1767dc4cab0e21fc1948텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)