显示views.py中的值到html



所以我要做的是显示一个值从我的Views.py在我的index.html。

Views.py

def index(request):
the_uploaded_excel_file = excel_upload.objects.order_by('-id').first()
excel_file_path_from_db = the_uploaded_excel_file.my_excel_file.path
print(excel_file_path_from_db)
wb = load_workbook(excel_file_path_from_db)
ws = wb["Sheet1"]
df = pd.read_excel(excel_file_path_from_db, engine='openpyxl', sheet_name='Sheet1', skiprows=1)

max_date_column = ws.max_column
last_date_entered = ws.cell(row=1, column=max_date_column).value
todays_date = datetime.date.today()
Date_difference = pd.date_range(start= last_date_entered, end=todays_date, freq='D')
print("last date entered was ")
print(last_date_entered)
print("todays date is ")
print(todays_date)
print("the days not done is")
print(Date_difference)
for date in Date_difference:
print(date)
return render(request, "index.html", {
'Date_difference': Date_difference
})

index . html

<h2>
{{ for date in Date_difference }}
{{ date }}
{{ endfor }}
</h2>

问题是,当我只做{{ Date_difference }}在我的index.html它显示了我想要的日期数组。但是当我试图把它们放在for循环它的页面不会加载。当我执行{{ Date_difference }}时,它给了我:

DatetimeIndex(['2021-01-28', '2021-01-29', '2021-01-30', '2021-01-31', '2021-02-01', '2021-02-02', '2021-02-03', '2021-02-04'], dtype='datetime64[ns]', freq='D')

但是当我执行for循环时,我得到了错误:

Could not parse the remainder: ' date in Date_difference' from 'for date in Date_difference'

for循环的语法错误。应该是像

这样的东西
{% for date in Date_difference %}
<p>{{ date }}</p>
{% endfor %}

最新更新