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

223
Views
Django how do we send a model into javascript and render it onto a template

I'm trying to pass a Django model into a template using javascript. I can't seem to filter or do anything with the QuerySet once I get the javascript to read it and pass it on to the template.

My views.py:

def displayDict(request):
    m = ChatStream.objects.filter(name = visitor_ip_address(request))
    last = m.latest('name')
    return render(request, 'chatStream.html',
    {"chat": m, "last": last})

my models.py:

class ChatStream(models.Model):
    bot = models.TextField()
    user = models.TextField()
    name = models.CharField(max_length=100, null=True)
    created_date = models.DateTimeField(auto_now_add=True)

My chatStream.html file:

<p id="demo2">I will display when two seconds have passed.</p>

<script>
    var data = "{{chat}}";
    var lastEntry = "{{last}}"
    } 

setTimeout(myTimeout1, 2000) 


function myTimeout2() {
  document.getElementById("demo2").innerHTML = "2 seconds " + data + "lastEntry" + lastEntry;
}

</script>

The result I get after 2 seconds:

2 seconds <QuerySet [<ChatStream: ChatStream object (31)>, <ChatStream: ChatStream object (32)>]>lastEntryChatStream object (31)

Instead of showing "<QuerySet [<ChatStream: ChatStream object (31)>....] " How do I show the text inside the model named ChatStream?...

I've tried:

<p id="demo2">I will display when two seconds have passed.</p>

<script>
    var data = "{{chat.user}}";
    var lastEntry = "{{last.user}}"
    } 

setTimeout(myTimeout1, 2000) 


function myTimeout2() {
  document.getElementById("demo2").innerHTML = "2 seconds " + data + "lastEntry" + lastEntry;
}

</script>

But the above displays nothing.

I've also tried

<p id="demo2">I will display when two seconds have passed.</p>

<script>
    var data = "{{chat | last }}";
    var lastEntry = "{{last}}"
    } 

setTimeout(myTimeout1, 2000) 


function myTimeout2() {
  document.getElementById("demo2").innerHTML = "2 seconds " + data + "lastEntry" + lastEntry;
}

</script>

but filtering in last throws an error that I can't negative index (and I have the latest version of Django running).

Thanks so much

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

ChatStream.objects.filter(...) returns a QuerySet which is a list of objects of the ChatStream model, not a single ChatStream model object.

If you want to retrieve a single object of the ChatStream model the use

m = ChatStream.objects.get(name=visitor_ip_address(request))
about 4 years ago · Juan Pablo Isaza Report
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!