base.htmlのところに検索バーを作成し、next_page1.htmlで表示させたいのですが、next_page1.htmlには飛んでいるのに検索結果が表示されません。どこが悪いのでしょうか。お願いします。
basehtml
1 2 <form action="" method="get"> 3 <form class="form-inline my-2 my-lg-0"> 4 <div class="ui action input" style="width: 100%;"> 5 <input type="text" placeholder="検索" value="{{query_params}}" name="q"> 6 <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> 7 </div> 8 </form> 9 </form>
urlspy
1urlpatterns = [ 2 re_path(r'^next_page1/$', views.IndexView.as_view(),name='next_page1'), 3 re_path(r'^base/$', views.IndexView.as_view(),name='base'), 4]
nextpage1
1{% for res in search_results %} 2 3 <div class="container"> 4 5 <div class="card-deck"> 6 <div class="card text-white bg-secondary mb-4"> 7 <div class="card-body"> 8 <xml id="toolbox"></xml> 9 <a class="card-title">{{res.subjectofmail}}</a> 10 <p class="card-text">{{res.mail_address}}</p> 11 <p class="card-text">{{res.date}}</p> 12 13 </div> 14 </div> 15 </div> 16 17</div> 18 19{% endfor %}
viewspy
1 2class IndexView(TemplateView): 3 template_name = 'next_page1.html' 4 5 6 7 def get_context_data(self, **kwargs): 8 context = super(IndexView, self).get_context_data(**kwargs) 9 10 if 'q' in self.request.GET: 11 12 13 query_params = self.request.GET.get('q') 14 context['query_params'] = query_params 15 16 17 self.template_name = 'next_page1.html' 18 19 q = Search.parse_search_params(query_params) 20 21 22 if q: 23 query = reduce(operator.and_, ( 24 Q(subjectofmail__contains=w) | Q(mail_address__contains=w) for w in q)) 25 search_result = information.objects.filter(query) 26 context['search_result'] = search_result 27 else: 28 return context 29
functionpy
1from typing import List 2 3 4# 検索フォーム 5class Search: 6 @staticmethod 7 def parse_search_params(words: str) -> List[str]: 8 search_words = words.replace(' ', ' ').split() 9 return search_words
回答1件
あなたの回答
tips
プレビュー