学习Django时,我在访问两个不同的值时遇到了问题。在
在def home(request
下的views.py
中,我添加了一个包含2个dictionary对象的列表,并在context
下传递它。它工作得很好,我将在我的front_page.html
模板中遍历字典,但是我还添加了一个简单的if title
变量,它只在变量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'})
我甚至尝试过 如何通过 正面_页面.html 基本.htmlcomp = {'title': 'Competitive'}
,并将comp
放入render()
。只有当render()
将多个字典值传递到模板{% 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 %}
<!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>
你只能有一个上下文dict,但是字典可以有任意多个键/值。在
^{1}$相关问题 更多 >
编程相关推荐