Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何解决Django模板上的字符串到数据转换问题_Python_Json_Django - Fatal编程技术网

Python 如何解决Django模板上的字符串到数据转换问题

Python 如何解决Django模板上的字符串到数据转换问题,python,json,django,Python,Json,Django,我在Django模板上遇到了字符串到数据的转换问题。我试图获取一个字符串(日期)并计算自该日期起的天数;所以它说的是“1个月,2个星期(从所有时间的最高日期算起)”。字符串到日期的转换工作正常,问题出在Django模板上。该模板当前仅显示for循环中返回的每个项目的json数据请求的最后日期。显然,我需要转换和显示每个特定记录的日期 我已经将json数据请求中的字符串格式化为日期对象 目前,只有列表中的最后一项作为days\u格式的变量发送 以下是Django视图定义: coin_list_ur

我在Django模板上遇到了字符串到数据的转换问题。我试图获取一个字符串(日期)并计算自该日期起的天数;所以它说的是“1个月,2个星期(从所有时间的最高日期算起)”。字符串到日期的转换工作正常,问题出在Django模板上。该模板当前仅显示for循环中返回的每个项目的json数据请求的最后日期。显然,我需要转换和显示每个特定记录的日期

我已经将json数据请求中的字符串格式化为日期对象

目前,只有列表中的最后一项作为
days\u格式的
变量发送

以下是Django视图定义:

coin_list_url = f"https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page={per_page_limit}&page=1&sparkline=false"

    request_coin_list = requests.get(coin_list_url)
    results_coin_list = request_coin_list.json()

    crypto_data_geckco = []


#string to date conversion

    def to_string(time):
        return time.strftime('%Y %M %d')

    def from_string(date, formatting):
        dt = datetime.datetime.strptime(date, formatting)
        return dt

#for loop
    for currency_gecko in results_coin_list:
        days_since_ath = currency_gecko['ath_date']
        days_since_ath_formatted = from_string(days_since_ath[:-14], "%Y-%m-%d")
        print('days since ath formatted', days_since_ath_formatted)
        crypto_data_geckco.append(currency_gecko)
        print("crypto_data_geckco", crypto_data_geckco)


    return render(request, 'crypto/latest.html', { 'crypto_data_geckco': crypto_data_geckco, 'days_since_ath_formatted': days_since_ath_formatted} )
然后在Django模板上:

{% for currency in crypto_data_geckco %}

    All-Time Percentage: {{ currency.ath_change_percentage|intword }}%

    and passing the "days_since_ath_formatted" variable only accesses the last item in the list:

    Days since ATH: {{ days_since_ath_formatted|timesince }}

{% endfor %}

{days\u since}}
应该为for循环中返回的每个项目显示不同的日期。目前,它仅为for循环中的每个项显示json列表中的最后一项。

看起来您并没有实际存储天数的值,因为在循环此列表时,您可能希望存储在dict中:

for currency_gecko in results_coin_list:
    currency = {}
    currency['ath_change_percentage'] = currency_gecko.ath_change_percentage
    currency['days_since_ath_formatted'] = from_string(days_since_ath[:-14], "%Y-%m-%d")
    print('days since ath formatted', days_since_ath_formatted)
    crypto_data_geckco.append(currency)
    print("crypto_data_geckco", currency)


return render(request, 'crypto/latest.html', { 'crypto_data_geckco': crypto_data_geckco, 'days_since_ath_formatted': days_since_ath_formatted} )
然后,在模板中,您将需要以下内容:

{% for currency in crypto_data_geckco %}

    All-Time Percentage: {{ currency.ath_change_percentage|intword }}%

    and passing the "days_since_ath_formatted" variable only accesses the last item in the list:

    Days since ATH: {{ currency.days_since_ath_formatted|timesince }}

{% endfor %}

我不能确切地说,但我猜你的结果列表是一个查询集,在这种情况下,你也可以直接向模型添加属性

我已经尝试了{currency.days},因为{u ath_格式化}timesince},但在Django模板上没有显示任何内容。而{days_ash_formatted | timesince}}显示“1个月,2周”,这是json数据请求结果列表中的最后一个元素。问题是代码没有在列表上迭代。它应该显示每个列表元素的每个唯一日期。我已经添加了一个更新,显示对视图的更改(此代码没有经过测试)货币['days_After_ath_formatted']=from_字符串(days_After_ath[:-14],%Y-%m-%d”),然后{currency.days_After_ath_formatted | timesince}在模板中的工作方式与您所说的一样!完美的