Home > Software design >  Django context not passing in HTML template
Django context not passing in HTML template

Time:12-31

I am not understanding where I am doing mistake. I am trying to passing context in my html page but why context isn't passing. see my code:

#views.py

def SupportReply(request):

    replyAgent = Contact.objects.all()
    
    context = {
      replyAgent:'replyAgent',
    }
    print(context)
    return render(request,'contact/support-reply.html',context)

urls.py

path('support-agent/',views.SupportReply,name='support-agent'),

HTML

{% for i in replyAgent %}

{{i.support_ticket}}

{%endfor%}

See my terminal result where all object printing from context:

[30/Dec/2021 17:41:29] "GET /support-agent/ HTTP/1.1" 200 6571
{<QuerySet [<Contact: Contact object (45)>, <Contact: Contact object (44)>, <Contact: Contact object (43)>, <Contact: Contact object (42)>, <Contact: Contact object (41)>, <Contact: Contact object (40)>, <Contact: Contact object (39)>, <Contact: Contact object (38)>, <Contact: Contact object (37)>, <Contact: Contact object (36)>, <Contact: Contact object (35)>, <Contact: Contact object (34)>, <Contact: Contact object (33)>, <Contact: Contact object (32)>, <Contact: Contact object (31)>, <Contact: Contact object (30)>, <Contact: Contact object (29)>, <Contact: Contact object (28)>, 
<Contact: Contact object (27)>, <Contact: Contact object (26)>, '...(remaining elements truncated)...']>: 'replyAgent'}

why I am not seeing any object in my HTML page? where I am doing mistake?

CodePudding user response:

You swapped the key and the value: the key should be the name of the variable, the value the queryset, so:

def SupportReply(request):
    replyAgent = Contact.objects.all()
    context = {
      'replyAgent': replyAgent
    }
    return render(request, 'contact/support-reply.html', context)
  • Related