Home > Software engineering >  Django website not showing dictionary values
Django website not showing dictionary values

Time:06-17

I am using VS code for django. I am not sure why but the default django website is not showing the expected output. The output is ignoring the dictionary values.

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
   context = {
       'name': 'Patrick',
       'age' : 23,
       'nationality':'British',

   }
   return render(request, 'index.html', context)
<h1>
    Welcome, {{name}}<br> You are {{age}} years old.
</h1>

(output in server website)

Welcome,
You are years old.

CodePudding user response:

Your code is correct but there might be two things that might cause the expected output not to show.

  1. Try saving your code on Vscode maybe you have not saved your code.
  2. Try breaking out of the server and re-run it again.

CodePudding user response:

try sending the context this way :

def index(request):
   context = {
       'name': 'Patrick',
       'age' : 23,
       'nationality':'British',

   }
   return render(request, 'index.html', {
    'context':context
   })

your HTML file should be something like this:

<h1>
   Welcome, {{context.name}}<br> You are {{context.age}} years old.
</h1>
  • Related