Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
Javascript async send query to Django query database then update the same html template

Assuming I have something need to be sent by javascript asynchronously without refreshing the page.

name.html

{%if names%}
   {%for name in names%}
       <h4>{{name}}</h4>
   {%endfor%}
{%endif%}

In name.html, I am going to send "Joe" to my Django backend for querying

let crsf = '{{csrf_token}}'
fetch('{%url "name"%}', {
credentials: 'same-origin',
headers: {
   'X-CSRFToken': crsf,
   'Content-Type': 'application/json',
   'Accept': 'application/json, text/plain, */*'},
method: 'POST', 
body: JSON.stringify({
         joe: "Joe", 
}),}).then(res=>console.log(res))

Now "Joe" has been sent to the backend

In views.py:

def name(request):
    names = Names.objects.all()
    paginator = Paginator(names, 100)
    page = request.GET.get('page')
    paged_names = paginator.get_page(page)
    context = {
                    'names':paged_names,
                }
    if request.method == 'POST':
        data = json.loads(request.body.decode("utf-8"))
        joe = data.get("joe")
        names = Names.objects.all()
        names = names.filter(name__iexact=joe)
        context = {
                    'names':names,
                }
        # return render(request, 'cool/names.html', context)

    return render(request, 'cool/names.html', context)

Now I have all names named "Joe" queried out from the database.

Question: How do I then render all "Joe"s in name.html and also making pagination work as intended assuming there are over 20k "Joes".

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!