我试图访问返回dictionary的class方法的值。我的函数如下所示:
class GetData:
def __init__(self, api_key, ip, interface):
self.api_key = api_key
self.asa_ip = ip
self.interface = interface
self.auth_headers = {'X-Auth-Token': api_key, 'Content-Type': 'application/json'}
def data(self):
req = requests.get('https://{}:/{}/entries'.format(self.ip, self.interface),
headers=self.auth_headers, verify=False)
json_response = json.loads(req.content)
data = {}
for items in json_response['items']:
...
return data #it return dictionary
在我的视图.py我的代码如下所示:
def class_data(request):
interface_name = request.session.get('interface')
ip = request.session.get('ip')
api_key = request.session.get('api_key')
peer = str(request.POST.get('Peer'))
class_data = DataForm(interface_name, api_key, ip)
return render(request, 'user/data.html', {'peer' : str(peer), 'class_data': class_data })
在我的数据.html我正在尝试访问GetData类中的数据:
<h5>Data</h5>
<p> {{ class_data.data.peer.pfs_group }}</p>
我没有得到任何错误,我在浏览器中看到一个空白页。我试着这样称呼班级:
{{ class_data.data[peer]['pfs_group'] }}
但是当我这么做的时候,我得到了一个错误:
django.template.exceptions.TemplateSyntaxError:
Could not parse the remainder: '[peer]['pfs_group']' from 'vpn_data.data[peer]['pfs_group']'
我做错了什么??你知道吗
问题是
peer
是一个变量,但当您将其用作键时,Django只将其视为一个文本字符串。你知道吗您应该在视图中执行查找,并将结果值传递给模板:
。。。你知道吗
相关问题 更多 >
编程相关推荐