0
1

More than 3 years have passed since last update.

【Django】フォームからviews.pyでクエリ結果をフィルタする方法

Posted at

環境

  • Django 2.0.2

実施内容

  • views.pyでformから送信された検索のクエリを受け取る
  • 本紹介では、変数名:query
views.py
 def get_queryset(self):
 q_word = self.request.GET.get('query')
 object_list = ServiceModel.objects.filter(
               Q(service_title__icontains=q_word) or Q(service_catch_copy__icontains=q_word))

-formタグでqueryを送信

<form method="get" action="">
 <div class="form-group">
  <input class="form-control" name="query" value="{{ request.GET.query }}" id="exampleFormControlInput1" type="text" placeholder="例)画像認識など">
 </div>
 <button type="submit" class="btn btn-info btn-block">検索</button>
</form>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1