Learning Django and I'm having an issue with accessing 2 different values.
In views.py
under def home(request
, I've added a list of 2 dictionary objects and passed it through under context
. It works perfectly, I'm about to for loop over the dictionary in my front_page.html
template, but I've also added a simple if title
variable which only works if I place {'title': 'Competitive'
} before the variable context
.
from django.shortcuts import render
# Create your views here.
owl = [
{
'title': 'Competitive'
},
{
'Team': 'Dynasty',
'Location': 'Souel Korea',
'Colors': 'Black & Gold',
},
{
'Team': 'OutLaws',
'Location': 'Houston',
'Colors': 'Green & Black',
}
]
def home(request):
context = {
"owl": owl
}
return render(request, 'overwatch_main_app/front_page.html', context, {'title': 'Competitive'})
def second(request):
return render(request, 'overwatch_main_app/about.html', {'title': 'Boom'})
I've even tried comp = {'title': 'Competitive'}
, and placing comp
into the render()
. It only works when I place comp
, or {'title': 'Competitive'}
before content
and then content
doesn't work.
return render(request, 'overwatch_main_app/front_page.html', comp, context)
return render(request, 'overwatch_main_app/front_page.html', {'title': Competitive'} , context)
How can I pass more than 1 value of dictionaries to my template via render()
front_page.html
{% extends 'overwatch_main_app/base.html' %}
{% block content %}
<h1> OverWatch</h1>
{% for o in owl %}
<p>{{o.Team}}</p>
<p>{{o.Location}}</p>
<p>{{o.Colors}}</p>
{% endfor %}
{% endblock %}
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
{% if title %}
<title>OverWatch {{title}}</title>
{% else %}
<title> OverWatch </title>
{% endif %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
You can only have one context dict, but a dictionary can have as many key/values as you like.
context = {
"owl": owl,
"title": "Competitive"
}
return render(request, 'overwatch_main_app/front_page.html', context)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With