[英]Struggles unpacking a two-dimensional list in template
I'm passing a bunch of data into my template but am having a hard time breaking apart a zipped list of items. 我正在将大量数据传递到模板中,但是很难拆分项目的压缩列表。 No matter what I try, I always get the following error.
无论我尝试什么,我总是会遇到以下错误。
Need 2 values to unpack in for loop;
需要2个值来解开for循环; got 0.
得到0。
Heres my code: 这是我的代码:
views.py views.py
import requests
from django.shortcuts import render
from django.http import HttpResponse
dictionary, words = [[], []], []
def home(request, username='johnny'):
template_name = 'main/index.html'
url = "https://www.duolingo.com/users/{}".format(username)
getUserData(url)
context = {
'username': username,
'dictionary': dictionary,
'words': words,
}
# print(context)
return render(request, template_name, context)
def getUserData(url):
response = requests.get(url)
userdata = response.json()
wordlists, explanations = [], []
for language in userdata['language_data']:
for index in userdata['language_data'][language]['skills']:
if index.get('levels_finished') > 0:
wordList = index.get("words")
wordlists.append(wordList)
explanations.append(index.get("explanation"))
for wordItem in wordList:
words.append(wordItem)
dictionary = list(zip(wordlists, explanations))
relevant template 相关模板
{% block content %}
{% for words, exp in dictionary %}
{{ words }}
{{ exp|safe }}
{% endfor %}
{% endblock %}
I've tested this code, it works. 我已经测试了此代码,它可以工作。
Once I refactored in Django to put wordLists in an array with explanations, things go to hell. 一旦我在Django中进行了重构,将wordLists放入带有解释的数组中,事情就死了。 If I
print(dictionary)
at the end of the method, the data shows in the console. 如果在方法末尾
print(dictionary)
,则数据将显示在控制台中。 Not sure what else I'm missing. 不知道我还缺少什么。
Your problem is with scope . 您的问题是范围 。 the dictionary(variable) which you are returning from
home
function(as context) and dictionary in getUserData
function are not in same scope. 从
home
函数(作为上下文)返回的字典(变量)和getUserData
函数中的字典不在同一范围内。 So whenever you are updating getUserData
method's dictionary, its not being updated in home
. 因此,无论何时更新
getUserData
方法的字典,都不会在home
对其进行更新。 I would not recommend your approach for dictionary as its using global variable . 我不建议您使用字典方法,因为它使用全局变量 。 I would recommend something like this:
我建议这样的事情:
def getUserData(url):
response = requests.get(url)
userdata = response.json()
wordlists, explanations, words = [], [], []
for language in userdata['language_data']:
for index in userdata['language_data'][language]['skills']:
if index.get('levels_finished') > 0:
wordList = index.get("words")
wordlists.append(wordList)
explanations.append(index.get("explanation"))
for wordItem in wordList:
words.append(wordItem)
return list(zip(wordlists, explanations)), words # return the value of dictionary from here
def home(request, username='johnny'):
template_name = 'main/index.html'
url = "https://www.duolingo.com/users/{}".format(username)
dictionary, words = getUserData(url) # catch value of dictionary
context = {
'username': username,
'dictionary': dictionary,
'words': words,
}
# print(context)
return render(request, template_name, context)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.