2017-07-06 50 views
0

異なるデータに対応する3つのフィールドを使用してモデルオブジェクトを検索する検索ページを作成しようとしています。ここでは以下の私のコードは次のとおりです。Djangoで複数フィールド検索を行う

models.py

class Schedules(models.Model): 
    course_name = models.CharField(max_length=128, choices=COURSE_NAME_CHOICES, default='a-plus') 
    start_date = models.DateField(auto_now=False, auto_now_add=False, default=datetime.date.today) 
    instructor = models.CharField(max_length=128, choices=INSTRUCTOR_CHOICES, default='adewale') 

views.py

def search_Schedule(request): 
    context_dict = {} 
    if request.method == 'POST': 
     query1 = request.POST['course_name_search'] 
     query2 = request.POST['start_date_search'] 
     query3 = request.POST['instructor_search'] 
     if query1: 
      results = Schedules.objects.filter(course_name__icontains=query1) 
      if query2: 
       results = results.filter(start_time=query2) 
       if query3: 
        results = results.filter(instructor__icontains=query3) 
        table = ScheduleTable(results) 
        if results.count(): 
         context_dict['table'] = table 
        else: 
         context_dict['no_results'] = query1 + ", " + query2 + ", and " + query3 
       else: 
        table = ScheduleTable(results) 
        if results.count(): 
         context_dict['table'] = table 
        else: 
         context_dict['no_results'] = query1 + " and " + query2 
      elif query3: 
       results = results.filter(start_time__icontains=query3) 
       table = ScheduleTable(results) 
       if results.count(): 
        context_dict['table'] = table 
       else: 
        context_dict['no_results'] = query1 + " and " + query3 
      else: 
       table = ScheduleTable(results) 
       if results.count(): 
        context_dict['table'] = table 
       else: 
        context_dict['no_results'] = query1 
     elif query2: 
      results = Schedules.objects.filter(start_time=query2) 
      if query3: 
       results = results.filter(instructor__icontains=query3) 
       table = ScheduleTable(results) 
       if results.count(): 
        context_dict['table'] = table 
       else: 
        context_dict['no_results'] = query2 + " and " + query3 
      else: 
       table = ScheduleTable(results) 
       if results.count(): 
        context_dict['table'] = table 
       else: 
        context_dict['no_results'] = query2 
     elif query3: 
      results = Schedules.objects.filter(instructor__icontains=query3) 
      table = ScheduleTable(results) 
      if results.count(): 
       context_dict['table'] = table 
      else: 
       context_dict['no_results'] = query3 
    return render(request, "schedule/search_schedule.html", context_dict) 

search_schedule.html

{% block main_content %} 
    <form method="post" action=""> 
     {% csrf_token %} 
     <label for="course_name_search">Course Name:</label> 
     <input type="text" name="course_name_search" id="course_name_search"> 

     <label for="start_date_search">Start Date:</label> 
     <input type="datetime" name="start_date_search" id="start_date_search"> 

     <label for="instructor_search">Instructor:</label> 
     <input type="text" name="instructor_search" id="instructor_search"><br> 
     <input type="submit" name="submit"> 
    </form> 
    <div id="result_panel"> 
     {% if table %} 
      {% render_table table %} 
     {% else %} 
      {% if no_results %} 
       No results returned for <q>{{ no_results }}</q> 
      {% else %} 
       Please enter a search 
      {% endif %} 
     {% endif %} 
{% endblock %} 

何らかの理由で、検索が動作するかどうIコース名フィールドまたは講師フィールドに入力しますが、複数のフィールドを入力すると機能しません。何らかの理由で、開始日フィールドは、日付の入力方法に関係なく、期間は動作しません。誰かがコードを入力する正しい方法で私を助けることができますか?ありがとうございました。

答えて

0

これが動作すれば試してみてください。

def search_Schedule(request): 
context_dict = {} 
if request.method == 'POST': 
    query1 = request.POST.get('course_name_search',None) 
    query2 = request.POST.get('start_date_search',None) 
    query3 = request.POST.get('instructor_search',None) 
    if query1: 
     results = Schedules.objects.filter(course_name__icontains=query1) 
     if query2: 
      results = results.filter(start_time=datetime.datetime.strptime(query2, "%d%m%Y").date()) 
      if query3: 
       results = results.filter(instructor__icontains=query3) 
       table = ScheduleTable(results) 
       if results.count(): 
        context_dict['table'] = table 
       else: 
        context_dict['no_results'] = query1 + ", " + query2 + ", and " + query3 
      else: 
       table = ScheduleTable(results) 
       if results.count(): 
        context_dict['table'] = table 
       else: 
        context_dict['no_results'] = query1 + " and " + query2 
     elif query3: 
      results = results.filter(instructor__icontains=query3) #changed this filter condition 
      table = ScheduleTable(results) 
      if results.count(): 
       context_dict['table'] = table 
      else: 
       context_dict['no_results'] = query1 + " and " + query3 
     else: 
      table = ScheduleTable(results) 
      if results.count(): 
       context_dict['table'] = table 
      else: 
       context_dict['no_results'] = query1 
    elif query2: 
     results = Schedules.objects.filter(start_time=datetime.datetime.strptime(query2, "%d%m%Y").date()) 
     if query3: 
      results = results.filter(instructor__icontains=query3) 
      table = ScheduleTable(results) 
      if results.count(): 
       context_dict['table'] = table 
      else: 
       context_dict['no_results'] = query2 + " and " + query3 
     else: 
      table = ScheduleTable(results) 
      if results.count(): 
       context_dict['table'] = table 
      else: 
       context_dict['no_results'] = query2 
    elif query3: 
     results = Schedules.objects.filter(instructor__icontains=query3) 
     table = ScheduleTable(results) 
     if results.count(): 
      context_dict['table'] = table 
     else: 
      context_dict['no_results'] = query3 
return render(request, "schedule/search_schedule.html", context_dict) 
0

あなたのif文最小限に抑えるために、あなたのフィルタリングのためのドリルダウンの方法を試みることができる:あなた以来、

course_name_search = request.POST.get('course_name_search', None) 
start_date_search = request.POST.get('start_date_search', None) 
instructor_search = request.POST.get('instructor_search', None) 

queryset = Schedules.objects.all() 

if course_name_search: 
    queryset = queryset.filter(course_name__icontains=course_name_search) 
if start_date_search: 
    queryset = queryset.filter(start_date=start_date_search) 
if instructor_search: 
    queryset = queryset.filter(instructor__icontains=instructor_search) 

# if none of the search params were filled in then return none 
if not course_name_search and not start_date_search and not instructor_search: 
    queryset = Schedules.objects.none() 

しかし、ここでいくつかの注意事項、および開始日とあなたの問題を助けるために何かをPOSTデータを直接読み込んでいる場合は、クエリの前にstart_date_searchを実際の日付に変換する必要があります。

POSTの代わりにGETを使用する方が良いかもしれないもう一つのことは、テンプレートで読み込み、選択した値を入力して 'no_results'に入力することです。

+0

心配しないで、私はそれを解決することができました。私はフィールドに間違ったものを入力していたようです。 –

関連する問題