Django forms for search

Hi

I have a form that has many records of prayer times. Each user would have many prayers times. Say Sami has 4 prayer records and Tomy has 3 prayer records (these are done). What i need is to create a form being the first field drop down list of the names (distinct) then button to search the previous table (that has the records) then i want to show the results in the same page template. How to do that?

so far here is what I did:
views.py

def reporting(request):

	records = []
	i = 0

	staff = prayers_records.objects.all().values('person_name').distinct()

	for record in staff:
		records.append(record['person_name'])

	# make the form to choose the user
    
	return render(request,'reporting.html',{'records':records})

the above code will get me the names (distict) and below:

reporting.html

{% for i in records %}
<h2>{{i}}</h2>
{% endfor %}

above i am able to get the names. Now how to create a form (not with fields from DB) to have a list of name and button to search? The search will then go to the table that has the users and prayers, get the records for the choosen user and display the records in the same page of the search button.

You can create a form with a ModelChoiceField. Your view then uses the value submitted by the POST to do whatever you want to do with it. (Either render the desired data on that page or return a redirect to the desired page using the PK of the selected value as a parameter.)

It’s up to you to decide what the UX will be.

I did some changes to the code:

views.py

def reporting(request):

	records = []
	staff = prayers_records.objects.all().values('person_name').distinct()

	for record in staff:

		records.append(record['person_name'])

	return render(request,'reporting.html',{'records':records})

	if request.method=="GET":  

		model = prayers_records
		template_name = "reporting.html"
		query = request.GET.get("q")
		object_list = prayers_records.objects.filter(Q(person_name__contains=query))
		#print(object_lis.valuet['prayer_name'])
		return HttpResponse(object_list, content_type='text/plain')

and in reporting.html:

<form id="reset-form"name="theform" role="form" autocomplete="off" class="form" method="get">
                        {% csrf_token %}

                    <div class="form-group">
                      <div class="input-group">
                        <span class="input-group-addon"><i >User Name</i></span>
                          <select name="q">
                            {% for i in records %}
                              <option value={{i}}>{{i}}</option>
                            {% endfor %}
                          </select>
                      </div>
                    </div>
                   <div class="form-group">
                     <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
                   </div>
                   </form>

the result i am getting is in the same site url:

so i need to get the result of the query:

query = request.GET.get("q")
object_list = prayers_records.objects.filter(Q(person_name__contains=query))

how to get the values of the results from the table such as name, time and date.

First, you’ve got this:

This is going to terminate your view at this point - no code after this is going to execute.

Here, you’re returning a queryset, not html. This data needs to be rendered by a template to HTML.

What you need to do is render one page with all the data you want on that page, passed to the template in the context.

how to do that? I tried to print the queryset

object_list = prayers_records.objects.filter(Q(person_name__contains=query)).values()
for course in object_list:
   names.append(course['person_name'])
print(names)

its not printing anything.

How do you render any template with data from a queryset?