amr_aly

search_in_database_MAHA

Jul 13th, 2021 (edited)
238
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###### in templates file.html
  2.  
  3. {% url 'your_app_name:your_function_name' as month_url %}
  4.  
  5. <form class="form-inline mt-2 mt-md-0 float-left" method="GET" action="{{month_url}}">
  6.  
  7.    <input class="form-control mr-sm-2" type="text" name='name' placeholder="Patient Name ..."
  8.     value="{{request.GET.name}}" aria-label="Search">
  9.    <button class="btn btn-secondary my-2 my-sm-0" type="submit" hidden>Search</button>
  10.                
  11. </form>
  12.  
  13. {% block content %}
  14.  
  15.     {% for obj in patient %}
  16.         <p>obj.databasefield1</p>
  17.         <p>obj.databasefield2</p>
  18.         <p>obj.databasefield3</p>
  19.     {% endfor %}
  20.  
  21. {% endblock %}
  22.  
  23.  
  24. ################### in views.py
  25. from django.shortcuts import render, redirect
  26. from django.urls import reverse
  27. from django.db.models import Q
  28. from .models import Visits
  29. from .tables import VisitsTable
  30. from datetime import date
  31.  
  32. def calculate_month_income(request):
  33.    
  34.     today = date.today()       # datetime.now()
  35.     month_income = Visits.objects.filter(visitdate__year=today.year, visitdate__month=today.month)
  36.     search_name = request.GET.get('name')
  37.     #search_month = request.GET.get('month')
  38.  
  39.     if search_name == None and search_month == None:
  40.         table = VisitsTable(month_income, exclude='addpresent')
  41.         table.paginate(page=request.GET.get("page", 1), per_page=10)
  42.  
  43.    # elif ('month' in request.GET) and request.GET['month']:
  44.        # result_month = Visits.objects.filter(Q(visitdate__year=today.year, visitdate__month=search_month))
  45.        # table = VisitsTable(result_month, exclude='addpresent')
  46.        # table.paginate(page=request.GET.get("page", 1), per_page=5)
  47.  
  48.     elif ('name' in request.GET) and request.GET['name'].strip():#
  49.         patient = Visits.objects.filter(Q(patient__name__icontains=search_name,
  50.                                         visitdate__year=today.year, visitdate__month=today.month))
  51.         return redirect(reverse('your_app_name:function_name'))
  52.         #table = VisitsTable(patient, exclude='addpresent')
  53.         #table.paginate(page=request.GET.get("page", 1), per_page=10)
  54.     else:
  55.         table = VisitsTable(month_income, exclude='addpresent')
  56.         table.paginate(page=request.GET.get("page", 1), per_page=10)
  57.    
  58.     context = {
  59.         'patient': patient,
  60.         #'month_table': table,
  61.     }
  62.     return render(request, 'reports/month_table.html', context)
  63.  
  64.  
  65.  
  66.  
RAW Paste Data