Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

javascript - Showing value in views.py to html

So what I am trying to do is to show a value from my Views.py in my 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>

the problem is that when I do just {{ Date_difference }} in my index.html it show an array of the dates i want. but When i try to put them in the for loop it the page wont load. when i do {{ Date_difference }} it gives me:

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')

But when I do the for loop i get the error:

Could not parse the remainder: ' date in Date_difference' from 'for date in Date_difference'
question from:https://stackoverflow.com/questions/66055481/showing-value-in-views-py-to-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The syntax for your for loop is wrong. It should be something like

{% for date in Date_difference %}
    <p>{{ date }}</p>
{% endfor %}



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...